c++ (5)
c++ (5)
c++ (5)
Polymorphism
Polymorphism relates to the ability of different subclasses in a hierarchy to respond to
the same command in their own ways. A hierarchy is a set of classes that relate to each
other under the principle of inheritance. For example, you might have a superclass
called "Shapes," with subclasses for "Circles" and "Squares."
Defining Class in C++
A class is defined in C++ using the keyword class followed by the name of the class. The following
is the syntax:
class ClassName {
access_specifier:
// Body of the class
};
Example
class ThisClass {
public:
int var; // data member
void print() { // member
method
cout << "Hello";
}
};
What is an Object in C++?
When a class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined in
the class, you need to create objects.
Syntax to Create an Object
We can create an object of the given class in the same way we declare the
variables of any other inbuilt data type.
ClassName ObjectName;
Example
MyClass obj;
Class Methods
Methods are functions that belongs to the class.
There are two ways to define functions that belongs
to a class:
•Inside class definition
•Outside class definition
No. Class Structure
Members of a class are private by Members of a structure are public
1 default. by default.
It is declared using It is declared using
2 the class keyword. the struct keyword.
It is normally used for data It is normally used for the
3 abstraction and inheritance. grouping of different datatypes.
Syntax:
Syntax:
class class_name {
struct structure_name {
4 data_member;
structure_member1;
member_function;
structure_member2;
};
A nested class is a class which is declared in another enclosing class. A nested
class is a member and as such has the same access rights as any other
member. The members of an enclosing class have no special access to
members of a nested class; the usual access rules shall be obeyed.
Constructor in C++ is a special method that is invoked
automatically at the time an object of a class is created. It is
used to initialize the data members of new objects generally.
The constructor in C++ has the same name as the class or
structure. It constructs the values i.e. provides data for the
object which is why it is known as a constructor.
Syntax of Constructors in C++
The prototype of the constructor looks like this:
<class-name> (){
...
}
Destructor is an instance member function that is invoked
automatically whenever an object is going to be destroyed. Meaning,
a destructor is the last function that is going to be called before an
object is destroyed.
Syntax to Define Destructor
The syntax for defining the destructor within the class:
~ <class-name>() {
// some instructions
}
Static data members are class members that are declared
using static keywords.
Syntax
className {
static data_type data_member_name;
.....
}
Static data members are useful for maintaining data
shared among all instances of a class.
The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features of
Object Oriented Programming in C++. In this article, we will learn about
inheritance in C++, its modes and types along with the information about how it
affects different properties of the class.
Syntax of Inheritance in C++
class derived_class_name : access-specifier base_class_name
{
// body ....
};
where,
•class: keyword to create a new class
•derived_class_name: name of the new class, which will inherit the base class
•access-specifier: Specifies the access mode which can be either of private, public or protected. If neither is specified,
private is taken as default.
•base-class-name: name of the base class.
Types Of Inheritance in C++
The inheritance can be classified on the basis of the relationship between the derived class
and the base class. In C++, we have 5 types of inheritances:
1.Single inheritance
2.Multilevel inheritance
3.Multiple inheritance
4.Hierarchical inheritance
5.Hybrid inheritance
1. Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one base class is
inherited by one derived class only.
Multiple Inheritance
Multiple Inheritance is a feature of C++ where a class can inherit from more than one class. i.e one subclass is
inherited from more than one base class.
Multilevel Inheritance
In this type of inheritance, a derived class is created from another derived class and that derived class can be
derived from a base class or any other derived class. There can be any number of levels.
Hierarchical Inheritance
In this type of inheritance, more than one subclass is inherited from a single base class. i.e. more than one
derived class is created from a single base class.
Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining
Hierarchical inheritance and Multiple Inheritance will create hybrid inheritance in C++
A virtual function (also known as virtual methods) is a
member function that is declared within a base class and is
re-defined (overridden) by a derived class. When you refer to
a derived class object using a pointer or a reference to the
base class, you can call a virtual function for that object and
execute the derived class’s version of the method.
A friend class can access private and protected members of other classes in
which it is declared as a friend. It is sometimes useful to allow a particular
class to access private and protected members of other classes. For example,
a LinkedList class may be allowed to access private members of Node.
We can declare a friend class in C++ by using the friend keyword.
Syntax:
friend class class_name; // declared in the base class
in C++, Operator overloading is a compile-time polymorphism. It is
an idea of giving special meaning to an existing operator in C++
without changing its original meaning.
C++ has the ability to provide the operators with a special meaning
for a data type, this ability is known as operator overloading.
Operator overloading is a compile-time polymorphism. For example,
we can overload an operator ‘+’ in a class like String so that we can
concatenate two strings by just using +. Other example classes
where arithmetic operators may be overloaded are Complex
Numbers, Fractional Numbers, Big integers, etc.
Type conversion is essential for managing different data types in C++. The C++
Course covers the various methods of type conversion, helping you
understand how to handle data types correctly. A type cast is basically a
conversion from one type to another. There are two types of type conversion:
1.Implicit Type Conversion Also known as ‘automatic type conversion’.
Explicit Type Conversion: This process is also called type casting and
it is user-defined.
Data Type Description