✔ C++ CHAPTERS:-
1. CPP-Introduction
2. CPP-OOPs Concepts
3. CPP-Statements
4. CPP-Programming Examples
5. CPP-Operators
6. CPP-Functions
7. CPP-Classes & Objects
8. CPP-Array Of Objects
9. CPP-Constructors
10. CPP-Destructors
11. CPP-Inheritance
12. CPP-Multiple Inheritance
13. CPP Hierarchical Inheritance
14. CPP-Important Points
1. CPP-Introduction
2. CPP-OOPs Concepts
3. CPP-Statements
4. CPP-Programming Examples
5. CPP-Operators
6. CPP-Functions
7. CPP-Classes & Objects
8. CPP-Array Of Objects
9. CPP-Constructors
10. CPP-Destructors
11. CPP-Inheritance
12. CPP-Multiple Inheritance
13. CPP Hierarchical Inheritance
14. CPP-Important Points
A constructor is special member function of a class which is used to initialize the member of a class whenever an object is first created. Constructor have some characteristic that makes it different from normal member function:
- Constructor have a same name equal to classname.
- Constructor doesn’t have any return data type.
- No access modifier [private, protected] is allowed. it follows only public modifier.
- No inheritance is possible with constructor.
Types of constructors:
- Default constructor [non parameterized].
- Copy constructor. [parameterized]
1. Default constructor :
- This type of constructor is automatically created when object of a class creates.
- This can be called non parameterized because it doesn’t contain parameters.
Class
{
{
public :
( )
{
{
}
};
Eg: class Sum
{
int num1,num2;
public:
Sum()
{
cout<<”default constructor called”;
}
}
};
void main( )
{
Sum s1;
}
2. Parameterized:- Arguments are passed to this type of constructor.
- When we make parameterized constructor in a class then default constructor not works.
#include
Class Sum
Class Sum
{
public:
Sum(int num1, int num2)
Sum(int num1, int num2)
{
cout<<”parameterized constructor”;
cout<
}
};
};
void main( )
{
Sum s1(20,30);
}
Constructors can be declared by two ways :- Sum s1(20,30);
- Sum s2=Sum(20,30);
- Overloading allow us to make a more than one function in a program. That can be done with constructors also.
- Multiple constructor can be made in a class, but they must be different in their no of arguments, data types of their arguments.
#include
class Sum
{
int a, b, c,sum1;
public:
Sum() // non parameterized constructor
{
a=0;
b=0;
c=0;
}
Sum(int x, int y, int z)
{
a=x;
b=y;
c=z;
}
void output()
{
sum1=a+b+c;
cout<<” sum is = “<
}
};
void main()
{
Sum s; //default constructor
Sum s1(50,20,30); //parameterized constructor called
S1.calculate();