Object Oriented Programming
Lecture# 3
Lecture# 3
Constructor
Today’s Lecture
• Access Specifiers
• Setter and getter
• Constructors
• Default constructor
• Parameterized constructor
• Constructor overloading
• Default parameter of the constructor
• Destructor
• Arrays and Pointers as members
Access Specifiers
Public access specifiers
Private access specifiers
Protected access specifiers
Public Access Specifiers
All the class members declared under the public specifier
will be available to everyone.
The data members and member functions declared as
public can be accessed by other classes and functions
too.
The public members of a class can be accessed from
anywhere in the program using the direct member access
operator (.) with the object of that class.
Code
#include<iostream>
using namespace std;
class student{
public:
string name;
int roll;
string display()
cout<<" Your name is: ";
return name;
};
int main()
student s1;
[Link]= "Umer Arshad Butt";
cout<<[Link]();
}
Private Access Specifiers
The class members declared as private can
be accessed only by the member functions
inside the class.
They are not allowed to be accessed directly
by any object or function outside the class.
Code
#include<iostream>
using namespace std;
class student{
private:
string name = "Umer Arshad Butt";
int roll;
public:
string display()
{
cout<<" Your name is: ";
return name;
}
};
int main()
{
student s1;
cout<<[Link]();
}
Setter and Getter
//setters
void setHeight(int h)
{
height = h;
}
void setWidth(int w)
{
width = w;
}
Setter and Getter
//getters
int getHeight()
{
return height;
}
int getWidth()
{
return width;
}
Class Activity
Private Members Type
Batcode (4 digit), Integer
Total_innings, n_out_innings,
runs,
Bestscore
Batavg float
Batname 10 character
Calavg() (Function to compute batsman avegerage ) float
Public Member:
readdata() void
detail: Function to accept value from Batcode, name, innings, not_out and invoke
the function Calcavg()
displaydata() Void
Detail: Function to display the data members on the screen
Constructor
A type of member function that is executed automatically when an
object of that class is created is known as constructor.
Properties of constructor:
1. The constructor member function have same name as name of
class.
2. The constructor no return type or it can’t return any value.
3. It is defined in classes to automatically initialize the data member/s.
4. Constructor may or may not have parameter/arguments
Constructor
Syntax of constructor:
The syntax of declaring constructor is as follows.
Name_of_class()
{
// constructor body.
}
Constructor
Types of constructors:
1. Default constructor / constructor without parameters
2. Parameterized constructor/ constructor with parameters
1-Default constructor /
constructor without parameters
• The constructor that takes no arugment/parameter is called default
constructor.
Program 1:
• Write a class that have a constructor displays a simple message on the screen whenever an
object of that class is created. Here in this example three objects of class.
#include<iostream.h
class Student
{
private:
int a;
public:
Student() //constructor member function, it will execute automatically when object will
//be created
Program 1:
{
cout<<"object of class Student is created"<<endl;
//initialize data members here
}//end of constructor Student
};//end of class Student
void main()
{
Student s1,s2,s3; // three times constructor function will be called.
}//end of main
Program 1:
Output:
object of class Student is created
object of class Student is created
object of class Student is created
Initializing data members of class using
default constructor member function
Program 2:
• Write a class that contains two integer data members which
are initialized to 10 and 20 when an object of that class is
created.
• It also have member function show_sum to add the two
integers and show the result.
Program 2:
#include<iostream.h>
class Hello
{
private:
int x,y;
public:
Hello() //constructor function
{
cout<<"I am constructor of Hello Class"<<endl;
x=10;
y=10;
} //end of member function Hello
Program 2:
void show_sum()
{
int sum;
cout<<"Value inside x is"<<x<<endl;
cout<<"Value inside y is"<<y<<endl;
sum=x+y;
cout<<"Sum of Two integers is "<<sum<<endl;
}//end of show_sum member function
};//end of class Hello
Program 2:
void main()
{
Hello h1;
h1.show_sum(); // 20
Hello h2;
h2.show_sum(); //20
}//end of main
Output:
i am constructor of Hello Class
value inside x is 10
value inside y is 10
sum of Two integers is 20
i am constructor of Hello Class
value inside x is 10
value inside y is 10
sum of Two integers is 20
2-Parameterized constructor/
constructor with parameters
• The method of passing parameters to constructor member function is same as
passing parameters to normal user define functions.
• The key difference is that the parameters are passed to the constructor member
function when the object is declared.
• The parameters are written in parenthesis along with the object name at the time
of declaration of object.
Note:
For parameterized constructor, we will have to give parameter at the time of object
creation.
Syntax of passing parameters to
constructor member function
The syntax of passing parameters to constructor is as follows:
object_name (parameters list);
Object_name: It indicates the name of the object to be created.
Parameters : It indicates the list of parameters passed to the constructor
Program:
Write a class that has marks and grade as data members. A constructor with two
parameters initializes data members with given values and member function show
displays the values of data members. You have to create two objects and display
the values inside them.
#include<iostream.h>
#include<conio.h>
class Student
{
private:
int marks;
char grade;
Program:
public:
Student(int m, char g) // constructor of Student with two parameters
{
marks=m;
grade=g;
}//end of constructor function
Program:
void show()
{
cout<<"Marks.....="<<marks<<endl;
cout<<"Grade.....="<<grade<<endl;
}//end of show
};//end of class student
Program:
void main()
{
Student s1(400,'B'); // passing parameters to object s1 of Student
Student s2(700,'A'); // passing parameters to object s2 of Student
[Link](); // 400,B
[Link](); //700, A
}//end of main
Class Activity
Write a class of Student that has age as data members. A constructor
with one parameter initializes data members input value if input value
is>7 and<30 with given value
Else displays message:
“You have entered Invalid value”
and member function show displays the age of student
. You have to create two objects and display the values inside them.
Program
#include<iostream.h>
class Student
{
private:
int age;
Program
public:
Student(int a) // constructor with one parameter
{
if(a>7&& a<30)
{
age=a;
}//end of if
else
{
cout<<"You input Invalid value"<<endl;
}//end of else
}//end of constructor function
Program
void show_age()
{
cout<<"Age of student is"<<age<<endl;
}
};//end of class
Program
void main()
{
Student s1(3); //input wrong value
s1.show_age(); //it will show garbage
cout<<"\n\n";
Student s2(9);
s2.show_age(); //it will show 9
}//end of main
Constructor overloading
Overloaded constructors have the same name
(name of the class) but a different number of
arguments.
Depending upon the number and type of
arguments passed, the corresponding constructor
is called.
student(float M, string N)
{
Header.h file student_name = N;
marks = M; char show_grade()
{
#pragma once }
return grade;
#include<iostream>
}
student(float M)
};c
using namespace std; {
marks = M;
class student { }
private:
string student_name; string name_show()
float marks; {
char grade; return student_name;
}
public:
student(string N, float M, char G) float show_marks()
{ {
student_name = N; return marks;
marks = M; }
grade = G;
}
[Link] file
#include<iostream>
#include "Header.h"
using namespace std;
int main()
student s1("Umer Arshad Butt ", 92, 'A'), s2(90.91, "Hanzla") , s3(100.11);
cout<<s1.name_show() << "\n";
cout << s1.show_marks() << "\n";
cout << s1.show_grade() << "\n";
cout << s2.show_marks() << "\n";
cout << s2.name_show() << "\n";
cout << s3.show_marks() << "\n";
cout << "\n";
system("pause");
}
Default parameter of the
constructor (header.h file)
#pragma once // Default parameterized Constructor
#include<iostream> student(float M=0.0, int ID= 0)
{
using namespace std; marks = M;
id = ID;
class student { }
private: float show_marks()
float marks; {
int id; return marks;
}
public:
// Default Constructor int show_id()
student() {
{ return id;
}
} };
Default parameter of the
constructor ([Link] file)
#include<iostream>
#include "Header.h"
using namespace std;
int main()
{
// second value automatically pick from class constructor
student s1(2);
cout << s1.show_marks() << "\n";
cout << s1.show_id() << "\n";
cout << "\n";
system("pause");
}
Destructor:
A type of member function that is executed automatically when an object of
that class is destroyed is called destructor.
1. No return type
2. Same name as class name but preceded by tilde sign (~)
3. Use to de allocate the memory,
4. It takes no parameters
5. Automatically executed when object dies..
6. Automatically called at compile time
Note: Per class one destructor is used
Syntax of destructor:
~class_name()
{
//destructor body
}
program code:
class Test
{
private:
int n=0;
public:
Test() //constructor
{
cout<<”I am constructor of Test class”<<endl;
}
program code:
~Test() //destructor
{
delete n;
cout<<”I am destructor of Test Class”<<endl;
}
}; // end of class
program code:
void main()
{
Test a, b;
}
Output:
I am constructor of Test class
I am constructor of Test class
I am destructor of Test class
I am destructor of Test class
Arrays and Pointers ([Link])
#include<iostream>
#include "Header.h"
using namespace std;
int main()
{
float *number, size;
cout << " Enter the array you want ";
cin >> size;
number = new float[size];
student s1;
[Link](" Umer Arshad ", 51, number , size);
[Link](size);
cout << "\n";
system("pause");
}
Arrays and Pointers (header.h)
#pragma once
#include<iostream>
using namespace std;
class student {
void getdata(int size)
private:
{
string name;
for (int i = 0; i < size; i++)
int id;
{
float marks[];
cout << " your number [ " << i << " ] = " << marks[i] ;
public:
cout << "\n";
void setdata(string N, int ID, float M[], int size)
}
{
}
name = N;
id = ID;
};
for (int i = 0; i < size; i++)
{
cout << " Enter your number [ " << i << " ] " << " \t";
cin >> marks[i];
}
}
Pointers as member (header.h)
#pragma once
#include<iostream>
using namespace std;
class student {
private:
int *id;
public:
void setdata(int ID)
{
id = new int(ID);
}
void getdata()
{
cout << " The user id is: " << *id;
}
};
Pointers as member ([Link])
#include<iostream>
#include "Header.h"
using namespace std;
int main()
{
student s1;
[Link](10);
[Link]();
cout << endl;
cout << "\n";
system("pause");
}