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

OOP_Lecture_7(Dynamic Object)

The lecture covers dynamic memory management in Object Oriented Programming (OOP) using UML diagrams, focusing on static and dynamic memory types. It explains how to create and manipulate dynamic objects and arrays using pointers and the new operator. Additionally, it includes code examples demonstrating the use of a Distance class and memory access through reference variables.

Uploaded by

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

OOP_Lecture_7(Dynamic Object)

The lecture covers dynamic memory management in Object Oriented Programming (OOP) using UML diagrams, focusing on static and dynamic memory types. It explains how to create and manipulate dynamic objects and arrays using pointers and the new operator. Additionally, it includes code examples demonstrating the use of a Distance class and memory access through reference variables.

Uploaded by

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

Object Oriented Programming (OOP)

------------------------------------------------------
Lecture Topic: Dynamic Object and UML Diagram
------------------------------------------------------
Instructor: Muhammad Aizaz Akmal
------------------------------------------------------
Lecture Oultine
0:Recape of previous Lecture
1:Type of Memory
-Static Memory
-Stack
-Automatically Destory
-Identifier Name containg container
-Dynamic Memory
-Heap Memroy
-Nameless
-Pointer used to manuplate
2:Dynamic Memory For Object
- How to hold address of object
Student{name,id}
Student s1;
Student *p1;
p1=&s1;
-To access the attribute of an objet using pointer we need to
use arrow operator (->) instead of dot operator (.)
p1.name; //Error
p1->name; //valid
-Alternate way to access object through pointer variable by using
dot operator(.)
(*p1).name; //valid
-How to create dynamic object
- use new operator
Student *ps = new Student;
ps->name;//
-How to create the array of object dynamically
size=5;
Student *ps = new Student[size];
Student s[10];

ps[0].name="Ali";
ps[1].id=25;
-----------------------
Mystring firstname; //Aizaz //5 //6
Mystring lastname; //Akmal //5 //6
firstname.con(lastname) //Aizaz Akmal //12
firstname.delete;
firstname.arr=new [length+lastname.getlength()+2] ;

3: Access dynamic memeory using Reference Variable


int i=2;
int& ri=i; //Alias
Distance& d1 = *(new Distance(10, 2.5));
d1.display();

---------------------------------------------
Lecture Code
---------------------------------------------
#include<iostream>
#include<string>
using namespace std;

class Distance{
private:
int feet;
float inch;
public:
Distance(int tfeet = 0, float tinch = 0) :feet(tfeet), inch(tinch){
cout << "default cons inovke." << endl;
}
Distance(Distance &obj)
{
cout << "copy invoke"<<endl;
feet = obj.feet;
inch = obj.inch;
}
~Distance()
{
cout << "Destructor invoke" << endl;

}
void setDistance(int tfeet, float tinch)
{
feet = tfeet;
inch = tinch;
}
void setFeet(int tfeet)
{
feet = tfeet;

}
void setInch(float tinch)
{
inch = tinch;
}
int getFeet()
{
return feet;

}
float getInch()
{
return inch;
}
void display()
{
cout << "Distance is : " << feet << "\'-" << inch << "\"" << endl;
}

};

int main()
{
//Distance d1(50, 3.5);
////d1.display();
//Distance *pd1 = &d1;
//cout << "Size of Distance Pointer: " << sizeof(pd1)<<endl;
//(*pd1).display();
/*Distance *pd = new Distance(20, 6.3);
pd->display();
pd->setInch(2.9);
pd->display();*/
/*
Distance *parr = new Distance[3];
parr[0].setDistance(10, 2.5);
parr[1].setDistance(20, 1.5);
parr[2].setDistance(25, 4.9);
parr[0].display();
parr[1].display();
parr[2].display();*/
/*int i = 10;
int& ri = i;
cout << "Address of i: " << (int)&i << endl;
cout << "Address of ri: " << (int)&ri << endl;
cout <<"value: "<< ri << endl;
ri = 25;
cout << "value: " << i << endl;
*/
/*Distance d = Distance(2,1.5);
d.display();*/

Distance& d1 = *(new Distance(10, 2.5));


d1.display();

system("pause");
return 0;
}

You might also like