Object Oriented Programming
Object Oriented Programming
W3 - L3
Agenda
Assessment
C++ Data types
Constructor
Default Constructor
Parameterized Constructor
Copy constructor
Constructor overloading
Private constructor
Static Constructor
Destructor
Examples
Example for Revision
Step#1
#include <iostream>
#include <string>
using namespace std;
int a = 5 ;
ABC a = 10 ;
// ABC is class_name
// a is object
Constructor is a special member function which is used to initialize the value of
variable inside an object.
Instead , To initialize the values of a object we have to create a method like input()
or we have to use object to initialize data members.
a.input()
a. age = 10 ;
a.name = “ali”;
We want to initialize the values to data members like normal variables without creating and
calling functions .
Major points:
class student
{
private:
int main()
{
MyClass myObj;
}
Output: (cont.)
Hello World!
Example 2:
write a class that contains two integer data members which are initialized to 100
when an object is created. It has a member function avg that displays the average
of data members.
Solution:
01/06/2024
#include<iostream.h> cout<<“x=“<<x<<endl;
#include<conio.h> cout<<“y=“<<y<<endl;
class Number cout<<“Average=“<<(x+y)/2<<endl;
{ }
private: };
int x,y; void main()
public: {
Number(){ Number n;
x=y=100; n.avg();
} getch();
void avg(){ }
Find Errors:
01/06/2024
Class A
Void main()
{ {
Private:
int a ; A obj;
A()
Obj.show();
{
a=5;
} }
void show()
{
cout<<a;
}
};
Solution:
01/06/2024
Class A Void main()
{
Private:
{
int a ;
Public:
A obj;
A()
{ obj.show();
a=5;
} Output: ?
void show()
}
{
cout<<a;
}
};
CONSTRUCTORS EXAMPLE
#include <iostream>
01/06/2024
using namespace std;
class Counter
{
private:
int count;
public:
Counter() : count(0) //constructor
{
/*empty body*/ }
void inc_count() //increment count
{ count++; }
int get_count() //return count
{ return count; } };
int main()
{
01/06/2024
Counter c1, c2; //define and initialize
cout << "\nc1=" << c1.get_count(); //display
cout << "\nc2=" << c2.get_count();
c1.inc_count(); //increment c1
c2.inc_count(); //increment c2
c2.inc_count(); //increment c2
cout << "\nc1=" << c1.get_count(); //display again
cout << "\nc2=" << c2.get_count();
cout << endl;
return 0;
}
Output:
01/06/2024
c1=0
c2=0
c1=1
c2=2
1.Default Constructor:
01/06/2024
A constructor that accepts no parameters is called default constructor.
Syntax:
Class_name()
{
// code
}
Example:
Default Constructor
01/06/2024
Class A Void main() Output:
{ {
Private: A obj; 10
int a ;
}
Public:
A()
{
a= 10 ;
cout<<a;
}
}
Task # 1
01/06/2024
Write a program that initialize two integer variables x and y, assign both variables
with value 100 by creating constructor member function inside the class , create
another member function that calculate average of these two numbers and display
it.
Solution:
01/06/2024
Void main ()
Class Number
{
{
Int x , y ;
Public: Number n ;
Number() n.avg() ;
{
x = y = 100} }
Void avg()
{
Cout<<“x is =” << x;
Cout<< “y is =” << y;
Cout << “average is =”x+y/2 ; }};
Task # 2
01/06/2024
Write a program that initialize two integer variables x and y, assign both variables
with value 100 by creating constructor member function outside the class , create
another member function that calculate average of these two numbers and display
it.
Solution:
01/06/2024
Class Number Number :: Number()
{ {
Int x , y ; x = y = 100}
Public:
Number(); Void main ()
{
void avg()
{ Number n ;
Cout<<“x is =” << x; n.avg() ;
Cout<< “y is =” << y;
Cout << “average is =”x+y/2 ; }}; }
2. Parameterized Constructor
01/06/2024
A constructor that accepts or receive parameters is called parameterized
constructor.
Syntax:
class_name(parameter1, parameter2 ---parameter n)
{
// code
}
Example:
Parameterized constructor:
01/06/2024
Class A Void show() Void main()
{ { {
Private: Cout<<a<<“”<<b; A obj(10,20);
int a, b ; } Obj.show();
Public:
}
A(int x , int y)
};
{ Output:
a= x; 10 20
b =y;
}
Task#3
01/06/2024
Write a program to create a parameterized constructor Wall() that has 2 parameters: double
len and double hgt.
The values contained in these parameters are used to initialize the member variables length and height.
When we create an object of the Wall class, we pass the values for the member variables as arguments.
01/06/2024
using namespace std; Wall(double len, double hgt) {
// declare a class { return length * height;
class Wall length = len; }
{ height = hgt; };
private: } int main()
double length; {
double height; 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;}
Output:
01/06/2024
Area of Wall 1: 90.3
Area of Wall 2: 53.55
Task#4:
01/06/2024
Write a class TV that contains the attributes Brand Name,
Model and Retail Price.
Write a method to display all attributes and a
method to change the attributes. Also write a
constructor to initialize all the attributes by using
parameterized constructor.
01/06/2024
Displaying objects before change…
Brand name: Samsung
Model: HDTV
Price: 45000
Displaying objects after change…
Brand name: Apple
Model: HDTV
Price: 55000
Step # 1
01/06/2024
Class TV
{
Private:
Char brandname[];
Char Model[];
Float retail_Price;
Public:
TV(char brand[] , char model1[] , float price);
Void change(char brand[] , char model1[] , float price);
Void display();
};
Step#2
01/06/2024
TV :: TV (char brand[] , char model1[] , float price)
{
Strcpy(Brandname , brand);
Strcopy(Model , model1);
Retail_price = price;
}
Step#3
01/06/2024
void TV :: change (char brand[] , char model1[] , float price)
{
Strcopy(Brandname , brand);
Strcopy(Model , model1);
Retail_price = price;
}
Step#4
01/06/2024
void TV :: display ()
{
Cout << “” Brand name is: << Brandname <<endl;
Cout << “” Model is: ” << model1 << endl;
Cout << “” retail price is : “ << retail_price ;
}
Step# 5 :
01/06/2024
void main ()
Test. Change(“Apple” , “HDTV” ,
{ “35000”) ;
TV test(“samsung” , “HDTV” , 25000) ;
Test. Display();
Test. Display();
} }