Constructors Continued : Review
Constructors Continued : Review
Review:
A constructor is a special member function of a class that
is used to initialize the data members of an object of the
class.
It is invoked automatically whenever an object of the
class is defined.
A constructor without any argument is called a default
constructor.
If a default constructor is not defined in a program, the
C++ compiler automatically creates one.
class Counter
// use of default constructor
{ private:
unsigned int count;
public:
Counter()
// default constructor
{count = 0;}
void setcount()
{count++;}
int getcount()
{return count;}
};
int main()
{ Counter c1, c2;
cout << c1 =
cout << \nc2 =
c1.setcount();
c2.setcount();
cout << c1 =
cout << \nc2 =
}
<< c1.getcount();
<< c2.getcount();
<< c1.getcount();
<< c2.getcount();
Output:
c1 = 0
c2 = 0
c1 = 1
c2 = 1
Constructor overloading:
A class can have multiple constructors. All the
constructors have the same name as the class, and they
differ only in their signatures.
class Account
// constructor overloading
{ private:
int accno; float balance;
public:
Account()
// constructor 1
{cout << Enter the account no. << endl;
cin >> accno;
cout << Enter balance << endl;
cin >> balance;}
Account(int an): accno(an), balance(0.0)
{ }
Account(int ac, float bal):
accno(ac), balance(bal)
{ }
// constructor 2
// constructor 3
void display()
{cout << Account number is << accno << endl;
cout << Balance is << balance << endl;
}
};
int main()
{Account acc1;
// uses constructor 1
Account acc2(100);
// uses constructor 2
Account acc3(200, 950.0); // uses constructor 3
acc1.display();
acc2.display();
acc3.display();
}
class Complex
// default arguments
{ private:
float real, imag;
public:
Complex(): real(0.0), imag(0.0)
// constructor 1
{
}
Complex(float rin, float imin = 0.0): // constructor 2
real(rin), imag(imin)
{
}
int main()
{ Complex c1;
// uses constructor 1
Complex c2(2.0); // uses constructor 2 with
default value for imag
Complex c3(2.5, 1.2); // uses constructor 2 without
any default values
c1.display(c1 = );
c2.display(c2 = );
c3.display(c3 = );
}
Output:
c1 = 0+i0
c2 = 2+i0
c3 = 2.5+i1.2
class Distance
// use of copy constructor
{ private:
int feet; float inches;
public:
Distance(int ft, float in): feet(ft), inches(in)
{ }
// constructor
Distance(const Distance & d): feet(d.feet), inches(d.inches)
{ }
// copy constructor
void getdist()
{cout << "Enter feet " ; cin >> feet;
cout << "Enter inches "; cin >> inches; }
void showdist()
{cout << feet <<"\' - " << inches << '\"' << endl;}
};
int main()
{ Distance d1(10, 5.5);
Distance d2(d1);
Distance d3 = d1;
cout << "d1 = " ; d1.showdist();
cout << "d2 = " ; d2.showdist();
cout << "d3 = " ; d3.showdist();
}
Output:
d1 = 10 5.5
d2 = 10 5.5
d3 = 10 5.5
The statement,
Distance d2(d1);
Destructors:
class Test
// use of destructor
{public:
Test()
// default constructor
{cout << The object is created <<endl; }
~Test()
// destructor
{cout << The object is destroyed <<endl; }
int main()
{ {Test x;
cout << Now x is alive << endl;}
{Test y;
cout << Now y is alive << endl;}
}
Constant Objects:
Like other data types, we can also define constant objects
of a class.
The syntax for defining a constant object is:
const ClassName ObjectName(parameters);
Example:
class Distance
{private: int feet; float inches;
public: ...
// public members of Distance
};
int main()
{ const Distance d1(20, 5.5);
...
}
Pointers to Objects:
Like other data types, we can define pointers to objects of
a class.
The syntax for defining a pointer to an object is:
ClassName *objectName;
Example: class Distance
{private: int feet; float inches;
public: ...
};
int main()
{Distance *dptr;
dptr = new Distance;
(*dptr).feet = 5; (*dptr).inches = 4.5;
//equivalent to dptr->feet = 5 and dptr->inches = 4.5;
}
class Distance
// objects as function arguments
{ private:
int feet; float inches;
public:
Distance(): feet(0), inches(0.0)
{ }
Distance(int ft, float in): feet(ft), inches(in)
{ }
void getdist()
{cout << "Enter feet " ; cin >> feet;
cout << "Enter inches "; cin >> inches; }
void showdist()
{cout << feet <<"\' - " << inches << '\"' << endl;}
void add_dist(Distance, Distance);
};
void Distance::add_dist(Distance dist1, Distance dist2)
{inches = dist1.inches + dist2.inches;
feet = 0;
if(inches >= 12.0) {inches -= 12.0; feet++; }
feet += dist1.feet + dist2.feet;
}
int main()
{ Distance d1(10, 5.5);
Distance d2(8, 9.7);
Distance d3;
d3.add_dist(d1, d2);
cout << "d1 = " ; d1.showdist();
cout << "d2 = " ; d2.showdist();
cout << "d3 = " ; d3.showdist();
}
Output:
d1 = 10' - 5.5"
d2 = 8' - 9.7"
d3 = 19' - 3.2
class Distance
// function returning objects
{ private:
int feet; float inches;
public:
Distance(): feet(0), inches(0.0)
{ }
Distance(int ft, float in): feet(ft), inches(in)
{ }
void getdist()
{cout << "Enter feet " ; cin >> feet;
cout << "Enter inches "; cin >> inches; }
void showdist()
{cout << feet <<"\' - " << inches << '\"' << endl;}
Distance add_dist(Distance);
};
Distance Distance::add_dist(Distance dist2)
{ Distance x;
x.inches = inches + dist2.inches;
if(x.inches >= 12.0) {x.inches -= 12.0; x.feet = 1; }
x.feet += feet + dist2.feet;
return x;
}
int main()
{ Distance d1(10, 5.5);
Distance d2(8, 9.7);
Distance d3;
d3 = d1.add_dist(d2);
cout << "d1 = " ; d1.showdist();
cout << "d2 = " ; d2.showdist();
cout << "d3 = " ; d3.showdist();
}
Output:
d1 = 10' - 5.5"
d2 = 8' - 9.7"
d3 = 19' - 3.2
class Student
// use of public static data member
{public:
static int count; // public static data member
Student() {++count;}
~Student(){--count;}
};
int Student::count = 0;
int main()
{ Student x, y;
cout << Now there are << x.count
{Student x, y, z;
cout << Now there are << x.count
cout << Now there are << x.count
Student z;
cout << Now there are << x.count
}
// Output: 2, 5, 2, 3 students
<< students.\n;
<< students.\n;}
<< students.\n;
<< students.\n;
class Student
// use of private static data member
{private:
static int count; // private static data member
public:
Student() {++count;}
~Student(){--count;}
int numSt() {return count;}
};
//Since count is private, we
int Student::count = 0;
int main()
{ Student x, y;
cout << Now there are << x.numSt()
{Student x, y, z;
cout << Now there are << x.numSt()
cout << Now there are << x.numSt()
Student z;
cout << Now there are << x.numSt()
}
// Output: 2, 5, 2, 3 students
<< students.\n;
<< students.\n;}
<< students.\n;
<< students.\n;
class Student
// use of static member function
{private:
static int count;
public:
Student() {++count;}
~Student(){--count;}
static int num() {return count;}
};
int Student::count = 0;
int main()
{ Student x, y;
cout << Now there are << Student::num()
{Student x, y, z;
cout << Now there are << Student::num()
cout << Now there are << Student::num()
Student z;
cout << Now there are << Student::num()
}
// Output: 2, 5, 2, 3 students
<< students.\n;
<< students.\n;}
<< students.\n;
<< students.\n;
class Count
// friend function
{private:
int x;
friend void set_x(Count &, int);
public:
Count(): x(0)
{
}
//constructor
void print() const
{ cout << x << endl;}
};
void set_x(Count &c, int val)
{c.x = val;}
int main()
{ Count counter;
cout << counter.x after instantiation: ;
counter.print();
set_x(counter, 8);
cout << counter.x after call to set_x : ;
counter.print();
}
Output: counter.x after instantiation: 0
counter.x after call to set_x: 8
class Distance
// friend function
{private:
int feet; float inches;
friend void set_dist(Distance &, int, float);
public:
Distance(): feet(0), inches(0.0)
{
}
//constructor
void showdist()
{cout << feet <<"\' - " << inches << '\"' << endl;}
};
void set_dist(Distance & d, int ft, float in)
{d.feet = ft; d.inches = in;}
int main()
{Distance d1;
cout << Distance d1 initially is ;
d1.showdist();
set_dist(d1, 10, 5.5);
cout << Distance d1 after calling set_dist is ;
d1.showdist();
}
Output: Distance d1 initially is 00
Distance d1 after calling set_dist is 10-5.5
Friend Class:
An entire class can be made a friend of another class.
In this case, if Class2 is a friend of Class1, then all the
member functions of Class2 are friends of Class1.
The syntax of declaring a friend class is:
class Class1
{...
friend class Class2;
...
};
The statement
friend class Class2;
class Class1
{friend class Class2;
// friend class
private : int x;
public :
void getdata()
{public :
{ Class1 obj1;
Class2 obj2;
obj1.getdata();
obj2.disp(obj1);}
class Class1;
class Class2
{public:
};
class Class1
{private : int x;
public :
void getdata()
{cout<<"Enter a number " << endl;
class Myclass
{private:
char chararray[20];
public:
void show_addr()
{cout<<My objects address is << this << endl;}
};
int main()
{ Myclass c1, c2, c3;
c1.show_addr();
c2.show_addr();
c3.show_addr();
}
Output:
My object's address is 0x22ff20
My object's address is 0x22ff00
My object's address is 0x22fee0
class Myclass
{private:
int x;
public:
void test()
{this->x = 11;
cout << this->x;}
};
int main()
{ Myclass c;
Output:
The value of x is 11