Professional Documents
Culture Documents
Constructors 1
Constructors 1
Constructors 1
Copy constructor
• it is for initializing objects using other objects of the
same class, for the T class:
T::T(const T &);
welcome
welcome
welcome
changing
class String
{ Shallow copy
private:
char *s;
int size;
public:
String(const char *str = NULL); // constructor
~String()
{ delete [] s; }// destructor
void print() { cout << s << endl; }
void change(const char *); // Function to change
};
str2.change(“distinct ");
int main
• { const int i = 10; const int j = i + 10; // works
fine i++; // this leads to Compile time error }
class St
{
public:
int i;
St(int x) // constructor
{
i = x;
}
objOne.fal(); // No error
objTwo.fal(); // No error
objOne.add(); // No error
// objTwo.add(); // Compile time error
}
Here, we can see, that const member function never changes data members of
class, and it can be used with both const and non-const objecta. But a const
object cannot be used with a member function which tries to change its data
members.
class Zee
{ int i; mutable int j;
public: Zee() { i = 0; j = 0; } mutable keyword
void fool() const
{ i++; // will give error
j++; // works, because j is mutable } };
int main() { const Zee obj; obj.fool(); }
mutable keyword is used with member variables of class, which we want to change
even if the object is of const type. Hence, mutable data members of
a const objects can be modified.
Friends of Classes
36
Properties of friend function
If we make the entire class as a friend the automatically all the member functions
of the class become friends. For class B to be a friend of class A.
The class A must explicitly declare class B is its friend in the public section
41
• class A
{
friend class B;
};
class B
{
};
class A
{
private:
int a,b;
public:
A()
{
a=10;
b=20;
}
friend class B;
};
class B
{
public:
B()
void showA(A obj)
{
cout<<”The value of a: ”<<obj.a<<endl;
}
void showB(A obj)
{
cout<<”The value of b: ”<<obj.b<<endl;
}};
main()
{
A x;
B y;
y.showA(x);
y.showB(x);}
Class friend to a specified class Member
Return an object from the function
class Complex
{
private:
int real;
int imag;
public: Complex(): real(0), imag(0) { }
void readData()
{ cout << "Enter real and imaginary number
respectively:"<<endl;
cin >> real >> imag; }
Complex addComplexNumbers(Complex comp2)
{
Complex temp;
temp.real = real+comp2.real;
temp.imag = imag+comp2.imag;
return temp; }
void displayData()
{
cout << "Sum = " << real << "+" << imag << "i"; }};
int main()
{ Complex c1, c2, c3;
c1.readData();
c2.readData();
c3 = c1.addComplexNumbers(c2);
c3.displayData();
return 0;
}
This pointer
• The ‘this’ pointer is passed as a hidden
argument to all nonstatic member function
calls and is available as a local variable within
the body of all nonstatic functions. ‘this’
pointer is a pointer that holds the memory
address of the current object.
• Member function of every object has access
to a pointer “this”.
class Test
{
private:
int x;
public:
void setX (int x)
{ this->x = x;}
void print()
{ cout << "x = " << x << endl; }};
int main()
{
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;}
To return reference to the calling
object
class Test
{
private:int x;
int y;
public:Test(int x = 0, int y = 0)
{ this->x = x;
this->y = y;
}Test &setX(int a)
{ x = a; return *this;
}
Test &setY(int b)
{ y = b; return *this;
}
void print()
{ cout << "x = " << x << " y = " << y << endl; }};
• int main()
• {Test obj1(5, 5);
• obj1.setX(10).setY(20);
• obj1.print();return 0;}