Sie sind auf Seite 1von 22

OBJECT ORIENTED PROGRAMMING (OOP) WITH C++

CONSTRUCTOR, DESTRUCTOR, NAMESPACE

AIUB. FALL 2017

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com
CONTENTS

ü Namespace (What and why?).


ü Constructor (What and why?)
ü Destructor (What and why?)
ü Use of Constructor and Destructor (From real life example).
ü Call sequence of Constructor and Destructor in C++. Why is this order??

ü 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

have to use namespace in order to avoid name conflicts!


#include <iostream>
using namespace std;

// first name space int main () {


namespace monitor_space { monitor_space::output();
void output() {
cout << "display texts to the monitor! :)" << endl; printer_space::output();
} network_space::output();
}
return 0;
// second name space }
namespace printer_space {
void output() {
cout << "prints texts to the printer! :)" << endl;
}
}

// third name space display texts to the monitor! :)


namespace network_space {
void output() { prints texts to the printer! :)
cout << "sends text over network! :)" << endl;
} sends text over network! :)
}
CONSTRUCTOR
CONSTRUCTOR – A EXAMPLE

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.

¡ Defining a constructor function:

¡ Name of the constructor function must be same as the class name.

¡ It has no return type.

¡ It must be declared as public.

¡ It may /may not have any arguments.

¡ It can be overloaded.

¡ Default constructor: A constructor that does not take any parameter is known as default constructor.

¡ Parameterized constructor: A constructor that takes parameters.


#include <iostream> Person::Person(){
#include <string> hands = 2;
legs = 2;
using namespace std; gender = "";
}
class Person{
//parametarized constructor
int hands; Person::Person(int h, int l, string gen){
int legs; hands = h;
string gender; legs = l; Person p hands: 2 Gender:
gender = gen; Person p1 hands: 2 Gender: Male
} Person p2 hands: 2 Gender: Female
public:
int Person::getHands(){
return hands;
Person(); //default constructor }
//parametarized constructor
Person(int h, int l, string gender); string Person::getGender(){
return gender;
}
int getHands();
string getGender();
int main(){
Person p; // Calls Defalut constructor
};
Person p1(2,2, "Male"), p2(2,2, "Female"); //Calls parametarized constructor
cout << "Person p hands: " << p.getHands() << " Gender: " << p.getGender() << endl;
cout << "Person p1 hands: " << p1.getHands() << " Gender: " << p1.getGender() << endl;
cout << "Person p2 hands: " << p2.getHands() << " Gender: " << p2.getGender() << endl;}
CONSTRUCTOR - EXAMPLE
Default Constructor
class ConstructorTest
{
int a;
public: int main()
ConstructorTest() {
{
ConstructorTest test;
a = 0;
cout<<"Default Constructor: " << a <<endl;
} ConstructorTest test1(100);
ConstructorTest(int x)
{
a = x; test.printValues();
cout<<"Parametarized Constructor: " << a << endl; test1.printValues();
} }

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;
}

int Rectangle::area (void) {


return (width*height);
}
1. class Rectangle { EXERCISE (WHAT IS THE OUTPUT?)
2. int width, height;
3. public:
4. Rectangle ();
5. Rectangle (int,int);
6. int area (void);
7. };
21. int main () {
8. Rectangle::Rectangle () { 22. Rectangle rect (3,4);
9. width = 5; 23. Rectangle rectb;
10. height = 5;
11. cout << "Default Contructor: " << width << "," << height << endl; 24. cout << "rect area: " << rect.area() << endl;
12. } 25. cout << "rectb area: " << rectb.area() << endl;
26. return 0;
13. Rectangle::Rectangle (int a, int b) { }
14. width = a;
15. height = b;
16. cout << "Parametrized Contructor: " << width << "," << height << endl;
17. }

18. int Rectangle::area (void) {


19. return (width*height);
20. }
COPY CONSTRUCTOR – OBJECT CLONING

¡ Constructor that creates an exact copy of itself.

¡ A constructor CAN take any data type or class as its parameter but CANNOT take its own type.

¡ A constructor CAN take its own reference.

¡ 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

¡ A destructor is used to destroy the object.

¡ It will be invoked automatically as soon as the life of the object is finished.

¡ It neither has any return type nor it takes any parameter.

¡ 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

¡ Destructors are called exactly in the opposite order of the constructor.

¡ 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: asiful.islam@aiub.edu
For Section C+B2: Send your assignment to the following email address: sabilah.nishat@gmail.com

Das könnte Ihnen auch gefallen