0% found this document useful (0 votes)
4 views32 pages

lec-4

Uploaded by

zaryabimran222
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)
4 views32 pages

lec-4

Uploaded by

zaryabimran222
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/ 32

Object Oriented

Lecture
Programing
4
Today’s Lecture

• Pointers to Object
• Copy Constructor
• Deep Copy vs Shallow Copy
• Const Class Data Members
• Const Class Member functions
• Const Object
• Constructor Initializer list
• this pointer
Pointer to Objects (Header.h)
#include<iostream> void getdata()
using namespace std;
{
class student
cout << " your name is: " << name ;
{
private:
cout << " your id is: " << id ;
string name; cout << " your marks is: " << marks ;
int id; }
float marks; };
public:
void setdata(string N, int ID, float M)
{
name = N;
id = ID;
marks = M;
}
main.cpp
#include<iostream>
#include<string> s2.setdata(nam, idd, s_marks);
#include "Header.h" s2.getdata();
using namespace std;
s1 = &s2;
int main()
{
s1->getdata();
int idd; (*s1).getdata(); // another way of calling
string nam; }
float s_marks;
student* s1, s2;
s1 = new student;
cout << " Enter your name: ";
getline(cin, nam);
cout << " Enter student id: ";
cin >> idd;
cout << " Enter subject marks: ";
Copy Constructor

• The copy constructor is used to initialize the


members of a newly created object by copying
the members of an already existing object.
• The copy constructor in C++ is used to copy
data from one object to another.
Methods of Copying Data

There are three methods to data from one object to


another.
1) example obj1 (50);
example obj2 (obj1); obj1

50
2) example obj2 (50);
data
copy obj2
example obj3;
50
obj3 = obj2;
data

3) example obj2 (50);


example obj3 = obj2;
Copy Constructor (Header.h)

#include<string>
#include<iostream>
using namespace std;

class student{
private:
string name;
int id;
public:
student() // Default Constructor
{
}
Continued..

student(string N, int ID) // Parameterized Constructor


{
name = N;
id = ID;
}
void display()
{
cout << " your name is:" << name << endl;
cout << " your id is:" << id << endl;
}

};
Copy Constructor (main.cpp)
#include<iostream>
#include<string>
#include"Header.h"
int main()
{
student s1("Umer Arshad Butt", 51);
s1.display();

// Copy Constructor
// Copy values from s2 to s1
student s2(s1);
s2.display();
}
Copy Constructor / Shallow copy

• In shallow copy, an object is created by simply copying the


data of all variables of the original object. This works well if
none of the variables of the object are defined in the heap
section of memory.
• If some variables are dynamically allocated memory from
the heap section, then the copied object variable will also
reference the same memory location.
• Example of shallow copy is same as simple copy
constructor. Then why do we need a deep copy
constructor?
Issue comes in the Copy constructor

• An object is created by copying data of all variables,


and it also allocates similar memory resources with the
same value to the object.
• Below is the reason why we need to go for deep copy
Copy constructor (Header.h)
#include<string>
string updatename()
#include<string.h>
{
#include<iostream>
*name = " Ch Ahmad Junaid ";
using namespace std; return *name;
class student{ }
private:
string *name; int updateid()
int *id; {
public: *id = *id * 100;

student() // Default Constructor return *id;


}
{
}
void display()
student(string N, int ID)
{
{
cout << " your name is: " << *name << endl;
name = new string;
cout << " your id is: " << *id << endl;
*name = N; }
id = new int;
*id = ID;
Copy constructor (main.cpp)
#include<iostream>
#include<string> // Update the s1 values
#include"Header.h"
s1.updatename();
#include<string.h>
s1.updateid();

int main() // Display the s1 value


{ s1.display();
student s1("Umer Arshad Butt", 51); cout << "\n";
s1.display();

cout << " It also update the s2 values


// Copy Constructor whereas we do not update the s2 value" <<
// Copy values from s2 to s1 endl;
cout << " copy constructor s2 executed"; // Display the s2 value
s2.display();
student s2(s1); }
s2.display();
cout << "\n";
Copy constructor (output)
Deep Copy (Header.h)
#include<string> string updatename() {
#include<string.h> *name = " Ch Ahmad Junaid ";
#include<iostream> return *name;
}
using namespace std; int updateid() {
*id = *id * 100;
class student{ return *id;
private: }
string *name; void display() {

int *id; cout << " your name is: " << *name << endl;

public: cout << " your id is: " << *id << endl;

student() // Default Constructor }

{ // Explicit Copy Constructor

} student(const student& access)


{

student(string N, int ID) // Parameterized Constructor name = new string;

{ *name = *(access.name);

name = new string; id = new int;

*name = N; *id = *(access.id);

id = new int; }

*id = ID; };
Deep copy (main.cpp)
#include<iostream>
#include<string> // Update the s1 values
#include"Header.h"
s1.updatename();
#include<string.h>
s1.updateid();

int main()
// Display the s1 value
{
s1.display();
student s1("Umer Arshad Butt", 51);
s1.display();
cout << "\n";

// Copy Constructor cout << " Now its not update the s2 values" << endl;

// Copy values from s2 to s1 // Display the s2 value

cout << " copy constructor s2 executed" << endl; s2.display();

student s2(s1); system("pause");


s2.display();
cout << "\n"; }
Deep Copy constructor (output)
Constant

Constant is something that does no change.


In C ,C++ we use the keyword const to make program
element constant.

const keyword may be used in many context in a C++


program.
const keyword can be used with:
 Variable
 Class Data Memebers
 Class Member functions
 Object
Constant Variables

• If you make any variable constant using const


keyword, you can not change it’s value.
• Constant variable must be initialized while declared.
• Once initialized, if we try to change its value, then
we will get compilation error. The following two cases
will give us an error since in both these cases, we are
trying to change the value of a const variable
Constant Variables

int main()
{
const int a = 5; // declaring and initializing a
const variable
a = 10; // this will give compilation error
return 0;
}
Constant Class data member
Data members are just variables declared inside a class.
Constant data members (Header.h)
#include<string>
volume(int R)
#include<string.h>
{
#include<iostream> rad = R;
using namespace std; }

class volume{ double formula()


private: {
const double pi = 3.14; double d;
int rad; d = 4 / 3;

public: d = d * pi * rad * rad * rad ;


return d;
volume()
}
{

};
Constant data members (main.cpp)
#include<iostream>
#include<string>
#include"Header.h"
#include<string.h>
int main()
{
volume v1(10);
double result = v1.formula();

cout << " voulme of sphere: ";


cout << result;
cout << "\n";
system("pause");

}
Constant Member Function of Class
• A const member function cannot change the value of any data member of the class and
cannot call any member function which is not constant.
• To make any member function const, we add the const keyword after the list of the
parameters after the function name.
class A
{
public:
int x;
void func() const
{
x = 0; // this will give compilation error
}
};
Note that A const object can only call a const member function.This is because a
const object cannot change the value of the data members and a const member
function also cannot change the value of the data member of its class. So, a const
Constant Member Functions

Constant member functions are those functions that are


denied permission to change the values of the data members
of their class.
To make a member function constant, the keyword “const” is
appended to the function prototype and also to the function
definition header.
Constant Member Functions

 You cannot change the value because


const is mentioned in the function.

 There is one way to modify it if you


mention mutable keyword in class
member.
Constant Member Function (Header.h)
#include<string>
#include<string.h> double formula() const
#include<iostream>
{
using namespace std;
class volume{
private: d = 4 / 3;
const double pi; d = d * pi * rad * rad * rad ;
int rad; return d;
mutable double d;
}
public:
// Constructor Initialization List When you
// declared constacnt data outside
};
volume(double P, int R): pi{P}, rad{R}
{
Constant Member Function of Class

• We cannot make constructors const

• The reason for this is that const objects initialize the values of
their data members through constructors and if we make the
constructor const, then the constructor would not change the
values of the data members and the object would remain
uninitialized.
Constant Objects of Classes

We can also make an object of class const. We make an object const by


writing the const keyword at the beginning of the object declaration as
shown below.
const A a(4);

• We made the object a of class A const by writing the const keyword


before the class name at the time of its declaration.
• A const class object is initialized through the constructor.
• Constant object can only call constant member functions of a class.

We cannot modify the data members of a const object.


Constant Objects of Classes

#include <iostream>
using namespace std;
class A int main()
{ {
const A a; // declaring const object 'a' of
public: class 'A'
int x; a.x = 10; // compilation error
A() return 0;
{ }
x = 0;
}
};
Constant
• Constant Member Function
Functions that are denied permission
• Constant Member Of a Class
We CAN NOT make constructor constant or const.
• Constant Object Of Classes
const A a (4)
Constant object can only call Constant Member
Functions of a class.
Constructor Initializer List Syntax

Constructorname(datatype value):datamember(value)
{
...
}
this pointer (*this)

• this is a keyword which is also a pointer


which points to the object which
invokes the member function of the
particular class.

You might also like