0% found this document useful (0 votes)
10 views19 pages

OOP Chapter 2 Lecture #1

Uploaded by

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

OOP Chapter 2 Lecture #1

Uploaded by

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

Chapter 2:

Constructor & Destructor

BY
Mujahid Jemal (MSc)
Lecturer at Department of Electrical &Electronics Technology
and ATTC Registrar
e-mail : [email protected]

06-Jun-23 1
Outline

 Constructor

 Types of constructors
 Exercise

06-Jun-23 2
Constructor
• A constructor is a special member function of a class that is
executed whenever we create new objects of that class.

• A constructor will have exact same name as the class itself and
it does not have any return type at all, not even void.

• Constructors can be very useful for setting initial values for


certain member variables.

• It Should be declared in public section.

06-Jun-23 3
06-Jun-23 4
Types of Constructors

i. Default Constructors

• A default constructor is a constructor that takes no arguments.

• It is the one which invokes by default when object of the class is


created.

06-Jun-23 5
Syntax:
class Class_ Name
{……//private members
Public:
…. //public members
Class Name () ;//constructor prototype};
Class Name:: Class Name ()//constructor definition
{ // constructor body definition }

06-Jun-23 6
Example
1. Write a program in C++ using class and object. When the member function
defined outside class body
Algorithm:
 Start
 Create a class Student
 Read the name and fee of student by default constructor
 Display the student data members by using display data()
function
 Stop.

06-Jun-23 7
#include<iostream>
using namespace std;
class student
{

char name[50];
double fee;
public:
student();
void display();};

06-Jun-23 8
student::student()
{ cout<<"Enter the Name:"; cin>>name;

cout<<"Enter the Fee:";


cin>>fee;}
void student::display()
{ cout<<" Student Name :"<<name<<endl;

cout<<" Tution Fee :"<<fee<<endl; }

int main()
{ student s;
s.display();
return 0;}

06-Jun-23 9
ii. Parameterized Constructors:
• It is possible to pass arguments to constructors.
• Typically, these arguments help initialize an object when it is
created.
• To create a parameterized constructor, simply add parameters
to it the way you would to any other function.
• When you define the constructor’s body, use the parameters to
initialize the object.

06-Jun-23 10
Syntax:
class Class_ Name
{……//private members
Public:
…. //public members
Class Name ( list of parameters) ;//constructor prototype};
Class Name:: Class Name (list of parameters)//constructor definition
{ // constructor body definition }

06-Jun-23 11
Example
// CPP program to illustrate parameterized constructors
#include <iostream>
using namespace std;
class Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1) { x = x1; y = y1; }
int getX()
{ return x;
}
int getY() { return y; }};

06-Jun-23 12
int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor


cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();

return 0; }

Output:

p1.x = 10, p1.y = 15

06-Jun-23 13
Exercise
1. Write a program in C++ using class and object. When the member function
defined outside class body
Algorithm:
 Start
 Create a class Interest
 Declare the private data members: principal amount(float p), rate of
interest(float r), number of year( float t) ,simple interest( float si)
and amount(float amount).
 Declare default constructor which enter data members and calculate
simple interest [si=(p*r*t)/100] and amount =si+p.
 Display data members and calculated data by using show() member
function
 Stop.

06-Jun-23 14
#include<iostream>
using namespace std;
// Create class Interest
class Interest
{
// private data members
private:// private access specifier
float p; // declaration of Principle Amount
float r; // declaration of Rate of Interest
float t; //declaration of Number of years
float si; //declaration of Interest
float amount;
public: Interest(); void show( ); };

06-Jun-23 15
Interest::Interest() {
cout <<" Principle Amount : ";
cin>>p ;
cout<<" Rate of Interest : ";

cin>>r; cout <<" Number of years : ";

cin>>t;
si= (p *r*t) /100;
amount = si + p; }

06-Jun-23 16
void Interest::show( ) // definition of read()
member function
{
cout<<"\n Principle Amount: "<<p;
cout <<"\n Rate of Interest: "<<r;
cout <<"\n Number of years: "<<t;
cout <<"\n Interest : "<<si;
cout <<"\n Total Amount : "<<amount;
}

06-Jun-23 17
int main ( )
{ Interest b ; b.show ( );
return 0;}

06-Jun-23 18
06-Jun-23 19

You might also like