2. A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its
class which is used to initialize some valid values to the data members of an object. It is executed
automatically whenever an object of a class is created. The only restriction that applies to the
constructor is that it must not have a return type or void. It is because the constructor is
automatically called by the compiler and it is normally used to INITIALIZE VALUES. The
compiler distinguishes the constructor from other member functions of a class by its name which
is the same as that of its class. ctorz is an abbreviation of constructor in C+
C++ constructor can be defined as a class in the same way as that of normal member functions
and can access any of its data members. The syntax for defining constructor inside the class body
is as follows:
6. Here, the function Wall() is a constructor of the class Wall. Notice that the constructor
has the same name as the class,
does not have a return type, and
is public
7. A constructor to which no arguments are passed is called the Default
constructor. It is also called a constructor with no parameters.
In the example above, Wall() is a default constructor.
Using the default constructor, data members can be initialized to
some realistic values in its definition even though no arguments are
specified explicitly.
C++DEFAULTCONSTRUCTOR
8. EXAMPLE 1
1. If we have not defined a
constructor in our class,
then the C++ compiler
will automatically create
a default constructor with
an empty code and no
parameters.
9. Initialize a student’s data ( university name,
batch, roll no) using a default constructor.
Use string to get university name;
TASK 1
10. In C++, a constructor with parameters is known as a parameterized constructor. This is the
preferred method to initialize member data.
C++PARAMETERIZEDCONSTRUCTOR
13. Using parameterized constructor, initialize a
person’s bank account data ( Account title,
Account number, Account total salary ). Hint:
Use string to get the account title.
TASK 2
14. 1. Using parameterized constructor, initialize the account salary
of a person in may , june and july. Using functions calculate
total salary. If the salary for each of these month is increased 30
percent, display the new total salary for these months
2. Write a program that defines a constructor to find the
maximum and minimum values for 3 numbers
HOME TASK
Editor's Notes
// C++ program to demonstrate the use of default constructor
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
public:
// create a constructor
Wall() {
// initialize private variables
length = 5.5;
cout << "Creating a wall." << endl;
cout << "Length = " << length << endl;
}
};
int main() {
// create an object
Wall wall1;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class studentdata
{
private:
string universityname;
int Batch;
char section;
public:
studentdata()
{
universityname= "IST";
Batch=13;
section='A';
cout<<"The university name of this student is "<<universityname<<endl;
cout<<"The Batch number of this student is "<<Batch<<endl;
cout<<"The section of this student is " <<section;
}
};
int main()
{
studentdata s1;
}
//Method 2:
#include<iostream>
#include<string>
using namespace std;
class studentdata
{
public:
string universityname;
int Batch;
char section;
studentdata()
{
universityname= "IST";
Batch=13;
section='A';
}
void printinitialdata()
{
cout<<"*********STUDENT's DATA FROM THE RECORD*******"<<endl;
cout<<"The university name of this student is "<<universityname<<endl;
cout<<"The Batch number of this student is "<<Batch<<endl;
cout<<"The section of this student is " <<section;
}
};
int main()
{
studentdata s1;
s1.printinitialdata();
}
#include <iostream>
using namespace std;
// declare a class
class Wall {
private:
double length;
double height;
public:
// create parameterized constructor
Wall(double len, double hgt) {
// initialize private variables
length = len;
height = hgt;
}
double calculateArea() {
return length * height;
}
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea() << endl;
return 0;
}
#include<iostream>
using namespace std;
class marks
{
private:
int physics;
int chemistry;
int bio;
public:
marks(int a, int b ,int c)
{
physics=a;
chemistry=b;
bio=c;
printmarks();
}
void newtotalmarks(int d , int e ,int f )
{
physics=physics+d;
chemistry=chemistry+e;
bio=bio+f;
printmarks();
}
void printmarks()
{
cout<<"Total marks in physics are "<<physics<<endl;
cout<<"Total marks in chemistry are "<<chemistry<<endl;
cout<<"Total marks in bio are "<<bio<<endl;
}
};
int main()
{
cout<<"***** Total Marks of 3 subjects after exam 1*******"<<endl;
marks s1( 23, 54 ,54 );
int a, b, c;
cout<<"Enter the marks of exam 2 of physics";
cin>>a;
cout<<"Enter the marks of exam 2 of chemistry";
cin>>b;
cout<<"Enter the marks of exam 2 of bio";
cin>>c;
cout<<"***** Total Marks of 3 subjects after exam 2*******"<<endl;
s1.newtotalmarks(a, b, c);
}
#include<iostream>
#include<string>
using namespace std;
class accountdata
{
private:
string accounttitle;
int accountnumber;
int totalsalary;
public:
accountdata(string a , int b , int c)
{
accounttitle=a;
accountnumber=b;
totalsalary=c;
displaydata();
}
void displaydata()
{
cout<<"Account title is "<<accounttitle<<endl;
cout<<"Account number is"<<accountnumber<<endl;
cout<<"Total saray till now is "<<totalsalary<<endl;
}
};
int main()
{
accountdata a("IST",2,3);
}