Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

1.

const Member Function


2. Static Object
3. Copy constructor
‘const’ member function
int main(){
const int i = 10;
const int j = i + 10; // works fine
i++; // this leads to Compile time error
}
Const class variable
class Test{
const int i;
public:
Test(int x):i(x) {} //initialized using constructor
void show(){cout<<"i="<<i<<endl;}
};
int main(){Test t(190);t.show(); }
Const function
• The idea of const functions is not to allow
them to modify the object on which they are
called. 
• It is recommended to make as many functions
const as possible so that accidental changes to
objects are avoided.
Const class member function
class A{
public: int x;
void func() const{
x = 0; // [Error] can’t modify object
variable
}
};
int main(){}
void Date::setMonth( int mn )
// constant_member_function {
#include<iostream> month = mn; // Modifies data member
using namespace std; }
class Date int main()
{ {
public: Date MyDate( 7, 4, 1998 );
Date( int mn, int dy, int yr ) const Date BirthDate( 1, 18, 1953 );
{ MyDate.setMonth(4); // Okay
month = mn; cout<<MyDate.getMonth();
day = dy; cout<<BirthDate.getMonth(); // Okay
year = yr; //BirthDate.setMonth( 4 ); // C2662 Error
} }
int getMonth() const; // A read-only function
void setMonth( int mn ); // A write function; can't be const
private:
int month,day,year;
};

int Date::getMonth() const


{
return month; // Doesn't modify anything
}
Static Object
What is the output?
class Test {
public: Test() {cout << "In constructor"<<endl; }
~Test() { cout << "In destructor"<<endl; }
};
void myfunc() { Test obj; }
int main() {
cout << "Start main()"<<endl; myfunc();
cout << "End main()"<<endl;
}
Answer:
Start main()
In constructor
In destructor
End main()
Now using static object
class Test {
public: Test() {cout << "In constructor"<<endl; }
~Test() { cout << "In destructor"<<endl; }
};
void myfunc() { static Test obj; }
int main() {
cout << "Start main()"<<endl; myfunc();
cout << "End main()"<<endl;
}
Answer: Static objects never die easily

Start main()
In constructor
End main()
In destructor
Parameterized constructor

• Constructor that take arguments are called parameterized


constructor.
• We must pass the initial values as arguments to the
constructor function when an object is declared , in two ways
1. Calling the constructor Explicitly
2. Calling the Constructor Implicitly
Example of Parameterized Constructor
class example When a constructor is parameterized, we
{ must pass the initial values as arguments to
private: the constructor function when an object is
int a; declared.
public:
example(int); //Parameterized Constructor Two ways Calling:
void display( ); • 1. Explicit
}; • example e1 = example(5);
example :: example(int x)
• 2. Implicit
{
• example e1(5);
a=x;
} • //Shorthand method method
void example :: display()
{
cout<<a;
}
Example of Parameterized Constructor

#include<iostream> main()
using namespace std; {
class integer integer n1(10,20);
{ integer n2= integer(20,30);
int m,n; n1.display();
public: n2.display();
integer(int , int); }
display();
{ 1. Explicit
cout<<m<<n; integer n2 = example(20,30);
} 2. Implicit
}; integer n1(10,20);
integer :: integer(int x, int y) //Shorthand method
{
m=x; n=y;
}
Copy Constructor

 A copy constructor is used to declare and initialize an object


from another object.

 For example, the statement:


example e2(e1);
will define the object e2 and at the same time initialize it to
the value of e1.

 The process of initializing through a copy constructor is


known as copy initialization.
Example of Copy Constructor
#include<iostream> void example::display()
using namespace std; {
class example cout<<a;
{ }
private:
int main()
int a;
{
public: example e1(5);
example(int); //Parameterized Constructor example e2(e1); //or, example e2=e1;
example(example &); //Copy Constructor e2.display();
void display(); return 0;
}; }
example::example(int x)
OUTPUT
{ 5
a=x;
}
example::example(example &p)
{ We cannot pass the argument by value to a
a=p.a; copy constructor.
}

You might also like