




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Usman Younis
Memory Leak!!
Classes and DMA (contd..)
#include
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
Usman Younis
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?
Usman Younis
THIS Pointer (contd..)
Function Definition
Function Call singer1.display( &singer1 ) ;
Usman Younis
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 }