0% found this document useful (0 votes)
21 views40 pages

Object Oriented Programming

Uploaded by

ranarishmile12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views40 pages

Object Oriented Programming

Uploaded by

ranarishmile12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

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;

class MyClass { // class


public: // Access specifier
int age; // Attribute (int variable)
string name; // Attribute (string variable)
};
Step#2
int main()
{
// Create an object of MyClass
MyClass myObj;
// Access attributes and set values
myObj.age = 15;
myObj.name = “Zahid";
// Print values
cout << myObj.age << "\n";
cout << myObj.name;
return 0;
}
WHAT IS CONSTRUCTOR?

 A constructor is a member function that is executed automatically whenever an


object is created.
 If you do not specify a constructor, the compiler generates a default constructor
for you (expects no parameters and has an empty body).
 You must supply the arguments to the constructor when a new instance is
created.
 It is normally defined in classes to initialize data members.
 Built in data types:

int a = 5 ;

 User defined-data types:


Like in class & object

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.

 We can’t initialize value using class name and object.


ABC a = 10 ;

 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:

1. Constructors name is same as the class name.


2. A constructor is automatically invoked.
3. A constructor can have only one access modifier that is public.
4. A constructor is never inherited nor overridden.
5. It does not have any return value.
6. Each and every C++ class has a constructor either it is provided by compiler by default or
explicitly created by user.
Why we use Constructor?

Constructor is a special member function of class which is


used to initialize an object.
The constructor can work as a normal function but it cannot
return any value.
CAN’T DO ...

class student
{
private:

int count = 0; // this is illegal


};
Where?

 Initialization of a class’s data members is done in the class’s constructor.


 The constructor of a class is a special method of the class that runs when a
variable of that class is instantiated
count()
{
count = 0;
}
This is not the preferred approach (although it does work).

Here’s how you should initialize a data member:


count() : count(0)
{}
The initialization takes place following the member function declarator but before the
function body. It’s preceded by a colon.
Example 1:
#include <iostream>
using namespace std;
class MyClass
{
public:
// Constructor
MyClass()
{
cout << "Hello World!";
}
};
Example 1: (cont.)

int main()
{

// Create an object of MyClass (this will call the constructor)

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.

// create atleast 2 objects


Solution
#include <iostream> public: double calculateArea()

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.

Create constructor and methods outside the class


Output:

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

Cout<< ” Displaying values after


Cout<< “Displaying objects before change…” change”

Test. Display();
Test. Display();

} }

You might also like