Classes and Dynamic Memory-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

This lecture was delivered by Prof. Usman Younis at Quaid-i-Azam University. This lecture covers following points of course Object Oriented Programming using C plus plus: Classes, Dma, Static, Data, Constructor, Destructor, Dynamic, Memory, Functions, Accessibility, Visibility, External

Typology: Slides

2011/2012

Uploaded on 07/31/2012

saqqi
saqqi 🇵🇰

4

(33)

40 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3/8/2011
1
Ob
j
ect Oriented
j
Programming using C++
Lecture – 7
Classes and Dynamic Memory
Classes and DMA
Classes with static data:
Constructor is used to initialize the data to the desired
Constructor is used to initialize the data to the desired
values, instead of garbage
Destructor has no real use
Classes can contain dynamic data:
Constructor can be used to automatically allocate space
Usman Younis
Destructor’s role is critical, such that it is required to de-
allocate the memory acquired at object’s instantiation
If Not??
Memory Leak!!
pf3
pf4
pf5
pf8

Partial preview of the text

Download Classes and Dynamic Memory-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Orientedj

Programming using C++

Lecture – 7

Classes and Dynamic Memory

Classes and DMA

„ Classes with static data:

‰‰ Constructor is used to initialize the data to the desiredConstructor is used to initialize the data to the desired

values, instead of garbage

‰ Destructor has no real use

„ Classes can contain dynamic data:

‰ Constructor can be used to automatically allocate space

Usman Younis

‰ Destructor’s role is critical, such that it is required to de-

allocate the memory acquired at object’s instantiation

‰ If Not??

Memory Leak!!

Classes and DMA (contd..)

#include using namespace std;g p ; class Singer { private: char* singer_name; char* album_name; public:

Usman Younis

Singer(char* s_name, char* a_name); ~Singer(); void display(); };

Classes and DMA (contd..)

Singer::Singer(char* s_name, char* a_name) {{ singer_name = new char[strlen(s_name) + 1]; strcpy(singer_name, s_name);

album_name = new char[strlen(a_name) + 1]; strcpy(album_name, a_name); }

Usman Younis

Singer::~Singer() { delete[] singer_name; delete[] album_name; }

Friend Functions and Classes

Data Accessibility

„ Default visibility of data in a class is private „ Additionally, data and functions can bedd ll d d f b made private when required

‰ Means it is not accessible out side the class

‰ Only member functions can access it

‰ Objects can provide an indirect access by passing

Usman Younis

object arguments to each others’ member

functions

‰ No external function can access the private data

Data Accessibility - Exceptions

„ C++ provides a flexibility to access the private data of a class by an external function, making it as a friendclass by an external function, making it as a friend function.

„ Similarly, a class can provide an access to its private data to another class, by making it as a friend class.

f d f l d f d d h

Usman Younis

„ A friend function or a class is defined outside the scope of a class which has made them friends.

Example

„ my_Class and student are two different classes „„ HoweverHowever, my_Class is made a friend of student in order my Class is made a friend of student in order to access all its private data „ A function display_students_from_main() is defined to display all the private data of my_Class, and hence is made friend of it „ An external function set_students_from_main() can easily access all the public data/functions of my Class

Usman Younis

easily access all the public data/functions of my_Class, just by using its reference „ See example hand out

THIS Pointer

„ How does C++ ensure that the proper object is referenced?is referenced?

‰ C++ compiler maintains a pointer, called the this

pointer.

‰ When a member function is called, this pointer

contains the address of the object, for which the

function is invoked

Usman Younis

function is invoked.

‰ Therefore, member functions of an object can

access the local data using this pointer

THIS Pointer (contd..)

„ Function Definition

void Singer::display(id Si di l ( SiSi nger* this* thi ))

// Singer* this is hidden to the programmer

//automatically added by compiler

„ Function Call singer1.display( &singer1 ) ;

Usman Younis

//&singer1 is hidden to the programmer

//automatically added by compiler

THIS Pointer (contd..)

„ Function Definition void Singer::display()void Singer::display() { //Normal Access cout<<"Singer name is : "<<singer_name<<endl; cout<<"Album name is : "<<album_name<<endl;

//Access using this pointer

Usman Younis

//Access using this pointer cout<<"Singer name is : "<<this->singer_name<<endl; cout<<"Album name is : "<<this->album_name<<endl;

//BOTH CASES WILL WORK }