You are on page 1of 10

Lecture#05

Object Oriented Programming


Topic:
Constructor examples
Distructor examples

ASMA JADOON
using namespace std;

class construct {
public: What is the type of constructor?
int a, b; Output of Program?

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
a: 10 b: 20
int main()
{
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 0;
}
#include <iostream>
using namespace std;
What is the type of constructor?
// declare a class
Output of Program?
class Wall {
private:
double length;

public:
// default constructor to initialize variable
Wall() { Creating a wall.
length = 5.5;
cout << "Creating a wall." << endl; Length = 5.5
cout << "Length = " << length << endl;
}
};

int main() {
Wall wall1();
return 0;
}
#include <iostream>
using namespace std;
class Wall {
private: What is the type of constructor?
double length; Output of Program?
double height;
public:
Wall(double len, double hgt) {
length = len;
height = hgt;
}
double calculateArea() { Area of Wall 1: 90.3
return length * height;
} Area of Wall 2: 53.55
};
int main() {
// create object and initialize data members
Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();
return 0;
}
class Car {
public:
string brand;
string model; What is the type of constructor?
int year; Output of Program?
Car(string x, string y, int z) { Write code of Constructor define outside
brand = x; the class?
model = y;
year = z;
}
};

int main() { // Constructor definition outside the class


Car carObj1("BMW", "X5", 1999); Car::Car(string x, string y, int z) {
Car carObj2("Ford", "Mustang", 1969); brand = x;
model = y;
cout << carObj1.brand << " " << carObj1.model
year << " " << carObj1.year << "\n";
= z;
cout << carObj2.brand << " " << carObj2.model
} << " " << carObj2.year << "\n";
return 0;
}

BMW X5 1999
Ford Mustang 1969
#include <iostream>
using namespace std;
class Test {
public: What is the type of constructor?
Test() { cout << "\n Constructor executed"; } Output of Program?
Add counter in it
~Test() { cout << "\n Destructor executed"; }
};

int main()
{
Test t, t1, t2, t3;
return 0;
}
#include <iostream>
using namespace std;
class Wall {
private: What is the type of constructor?
double length; double height; Output of Program?
public:
Wall(double len, double hgt) {
length = len; height = hgt;}
Wall(Wall &obj) {
length = obj.length;
height = obj.height;}

double calculateArea() {
return length * height; }}; Area of Wall 1: 90.3
int main() { Area of Wall 2: 90.3
Wall wall1(10.5, 8.6);
Wall wall2 = wall1; // copy contents of wall1 to wall2
cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
cout << "Area of Wall 2: " << wall2.calculateArea();}
class Demo {
private:
int num1, num2;
public:
Demo(int n1, int n2) { What is the type of constructor?
num1 = n1; Output of Program?
num2 = n2;
}
Demo(const Demo &n) {
num1 = n.num1;
num2 = n.num2;
} /tmp/MGkfgLCWgz.o
void display() {
cout<<"num1 = "<< num1 <<endl; num1 = 10
cout<<"num2 = "<< num2 <<endl;
} num2 = 20
};
int main() { num1 = 10
Demo obj1(10, 20);
Demo obj2 = obj1; num2 = 20
obj1.display();
obj2.display();
return 0;
}
// C++ program to demonstrate constructor overloading
#include <iostream>

Constructor Overloading:
using namespace std;
class Person {
private:
int age;
public: int main() {
• more than one constructor in a class with same name with different
Person listperson2(45);
person1, of arguments.
Person() {
age = 20;
• Overloaded constructors essentially have the same name (exact name of the class) and different by number
} cout << "Person1 Age = " << person1.getAge() << endl;
and type of arguments. cout << "Person2 Age = " << person2.getAge() << endl;
Person(int a) {
age
• A= a;
constructor is called depending upon the number and type of arguments passed.
} return 0;
• While creating the object, arguments must be passed }to let compiler know, which constructor needs to be
int getAge()
called.{
return age;
}
};
private:
double length; int main() {
double breadth; /tmp/8qj4HsXWTP.o Room room1, room2(8.2, 6.6), room3(8.2);

public: When no argument is passed: cout << "When no argument is passed: " << endl;
Room() { cout << "Area of room = " << room1.calculateArea() <<
length = 6.9; Area of room = 28.98 endl;
breadth = 4.2;
} cout << "\nWhen (8.2, 6.6) is passed." << endl;
Room(double l, double b) { cout << "Area of room = " << room2.calculateArea() <<
length = l; When (8.2, 6.6) is passed. endl;
breadth = b;
} Area of room = 54.12 cout << "\nWhen breadth is fixed to 7.2 and (8.2) is passed:"
// 3. Constructor with one argument << endl;
Room(double len) { cout << "Area of room = " << room3.calculateArea() <<
length = len; endl;
breadth = 7.2; When breadth is fixed to 7.2 and (8.2) is passed:
} return 0;
double calculateArea() { Area of room = 59.04 }
return length * breadth;
}
};

You might also like