Object Oriented Programming (Oop) With C++: Constructor, Destructor, Namespace
Object Oriented Programming (Oop) With C++: Constructor, Destructor, Namespace
ü Sources to practice.
NAMESPACE IN C++
NAMESPACE – WHAT AND WHY?
Consider a situation, when two persons with the same name, Zara, in the same class. Whenever
we need to differentiate them definitely we would have to use some additional information along
with their name.
Same situation can arise in your C++ applications. For example, you might be writing some code
that has a function called xyz() and there is another library available which is also having
same function xyz(). Now the compiler has no way of knowing which version of xyz() function
you are referring to within your code.
NAMESPACE – HOW?
Lets consider you want to implement three output() functions that will perform output operations
but for three different output devices, namely, monitor, printer and over network.
If you want to keep same name for the three functions which is output() in this case, you must
When a human child is born, it born with some default properties. These properties should/must be
there when they are born.
For instance, physical properties (hands, mouth, feet, eyes…), gender and others.
After being born their other properties are set.. E.g., name, religion, ethnicity and others..
Providing all the default properties while an object is created (just as human child) is done with
constructor function in OOP.
CONSTRUCTOR – A FUNCTION
¡ A constructor is a member function of a class which is called whenever an object is created.
¡ It can be overloaded.
¡ Default constructor: A constructor that does not take any parameter is known as default constructor.
void printValues()
{ Parameterized
cout<<"Just Printing: " << a << endl;
Constructor
}
};
CONSTRUCTORS
¡ If any constructor is not written then the compiler automatically takes a default constructor.
¡ If any non default constructor is written then the default constructor is not added by the
compiler. So, in such case, a default constructor should be written by the programmer.
EXAMPLE (WHAT IS THE OUTPUT?)
class Rectangle {
int width, height; int main () {
public: Rectangle rect (3,4);
Rectangle (int,int); Rectangle rectb;
int area (void);
}; cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
Rectangle::Rectangle (int a, int b) { return 0;
width = a; }
height = b;
cout << "Parametrized Contructor: " << width << "," << height << endl;
}
¡ A constructor CAN take any data type or class as its parameter but CANNOT take its own type.
¡ A constructor that takes its own reference as parameter is known as copy constructor.
COPY CONSTRUCTOR
class Student
{
int id;
string name;
public: int main ()
Student() { } {
Student(int i, string n) Student st1 (123, "Mottalib");
{
Student st2(st1);
id=i;
name=n;
}
st1.printStInfo();
Student(const Student &st) //copy const st2.printStInfo();
{ return 0;
id=st.id; }
name=st.name;
cout<<"In copy constructor" << endl;
}
void printStInfo(){
cout << "id: " << id << " name: " << name << endl;
}
};
COPY CONSTRUCTOR (WHAT IS THE OUTPUT?)
class A
{ int main()
int a; {
public: A ob1(10);
A() { }
A(int x) A ob2=ob1;
{
a=x; A ob3(ob1);
}
A(const A &ob) ob1.print();
{ ob2.print();
a=ob.a; ob3.print();
cout<<a<<endl; return 0;
}
}
void print(){
cout << “Values is: ” << a << endl;
}
};
DESTRUCTORS
¡ It has same name as the class name but begins with a Tilde (~) sign.
¡ As like constructor, a default destructor is always written if not explicitly mentioned by the
programmer.
DESTRUCTORS
¡ Usually destructors are written only when memory is dynamically allocated in constructors.
EXAMPLE
class A int main()
{ {
int a; A a1,a2;
static int count;
{
public:
A() cout<<"block 1\n";
{ A a3;
count++; }
cout<<"Construction of object:"<<count<<endl;
A a4;
}
~A() {
{ cout<<"block 2\n";
cout<<"Destruction of object:"<<count<<endl; A a5;
count--; }
}
A a6;
}; return 0;
int A::count=1; }
OUTPUT
Construction of object:2
Construction of object:3
block 1
Construction of object:4
Destruction of object:4
Construction of object:4
block 2
Construction of object:5
Destruction of object:5
Construction of object:5
Destruction of object:5
Destruction of object:4
Destruction of object:3
Destruction of object:2
ASSIGNMENT 4
Write a class Box that has three attributes to define a box. They are height, width and depth. The
default size of a box is 2x2x2. However, it is also possible to create custom size box as well. Write
appropriate constructor functions to create box objects with above functionalities.
Additionally, write appropriate methods to get the height, width and depth of a box.
Now, create a box object (box1) that will have the default size. Then create another box object (box2)
from this box object (box1). Then print the size of both the boxes.
Submission detail:
- Deadline:24.10.2017
- Email attachment (.txt file)
- Email Subject: assignment4_PL2
- Write your name, id and section in the email body
For Section A+B1: Send your assignment to the following email address: [email protected]
For Section C+B2: Send your assignment to the following email address: [email protected]