Lecture-9 (Constructors)
Lecture-9 (Constructors)
class classname
{
private:
member variables
public:
member functions
};
1
Recap: Class Example
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
class smallobj { //define a class
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is " << somedata << endl;
}
}; 2
Recap: Class Example
int main() Objects of Class
{
smallobj s1, s2;
Setting Values
via objects
s1.setdata(1066);
s2.setdata(1776);
Showing Values
via objects
s1.showdata();
s2.showdata();
return 0;
}
3
What is a constructor?
Constructor is used to initialize the objects of
a class
Constructor is used to ensure that object is in
well defined state at the time of creation
Constructor is automatically called when the
object is created
Constructor are not usually called explicitly
What is a constructor?
Constructor is a special function having same
name as the class name
Constructor does not have return type
Constructors are commonly public members
Class
Class Student
Student
{ Class
Private:
int id;
Public:
Student
{ Default
Constructor
}
};
6
Comments on constructors
Constructor is a function in every class which is called
automatically when class creates its object
Basically it helps in initializing data members of
the class
A class may have multiple constructors
If you do not specify a constructor, the compiler
generates a default constructor for you (expects no
parameters and has an empty body).
Default Constructor
Constructor without any argument is called
default constructor
If we do not define a default constructor the
compiler will generate a default constructor
This compiler generated default constructor
initialize the data members to their default
values
Example
class Student
{
int rollNo;
char *name;
float GPA;
public:
… //no constructors
};
Example
Compiler generated default constructor
{
rollNo = 0;
GPA = 0.0;
name = NULL;
}
Default Constructor
c1.inc_count(); //increment c1
c2.inc_count(); //increment c2
c2.inc_count(); //increment c2
or
Counter() //constructor
{
count = 0;
}
Multiple members initialize
If multiple members must be initialized,
they’re separated by commas.
The result is the initializer list (sometimes
called by other names, such as the member-
initialization list).
counter() : m1(7), m2(33), m2(4)
{
/*empty body*/
}
What is the output?
#include<iostream.h> void showData()
class Distance {
{ cout<<feet<<" feet and
private: "<<inches<<"inches\n";
int feet; }
float inches; };
public:
Distance () void main()
{ {
feet=0; Distance d1;
inches=0.0; d1.showData();
}
d1.getData();
void getData() d1.showData();
{
cout<<"Enter Feet\n";
cin>>feet; }
cout<<"Enter Inches\n";
cin>>inches;
}
Same Name as the Class
There are some unusual aspects of constructor
functions.
First, it is no accident that they have exactly the same
This is one way the compiler knows they are
constructors.
Second, no return type is used for constructors. Why
not?
Since the constructor is called automatically by the
system, there’s no program for it to return anything
Return value wouldn’t make sense. This is the second
way the compiler knows they are constructors.
Rectangle Example with object
Rectangle Example with constructor
Example 3: Output?
Declaring member function outside the class