OOP_Lecture_7(Dynamic Object)
OOP_Lecture_7(Dynamic Object)
------------------------------------------------------
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] ;
---------------------------------------------
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();*/
system("pause");
return 0;
}