






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: Structures, Organize, Store, Data, Single, Entity, User, Types, Memory, Arrays, Dynamic, Creation
Typology: Slides
1 / 12
This page cannot be seen from the preview
Don't miss anything!







Usman Younis
Usman Younis
Usman Younis
Overloading DiffDiff erent functionality is performed using thet f ti lit i f d i th functions having a same name However, they are differentiated with number/types of arguments E.g., void function();
Usman Younis
void function(); void function(int a); void function(int a, int b);
Overriding FF unction in a derived class that has the sameti i d i d l th t h th name and parameter list (usually) as a function in the base class Typically used to replace a function in base class with different actions in derived class When a function is overridden all objects of
Usman Younis
When a function is overridden, all objects of derived class use the overriding function You can use the original function in the base class, by using the scope resolution operator
Another Example:
Usman Younis
Case 1: class Derived: public A, public B { class A { public: float Pi() { return 3.14; } };
void main() { Derived Object;
Usman Younis
class B { public: float Pi() { return 3.14; } };
//Use cout<<Object.A::Pi()<<endl; //OR cout<<Object.B::Pi()<<endl; }
Diamond Shaped Inheritance Tree
Usman Younis
Diamond Shaped Inheritance Tree (Solution)
class Matter { public: float mass; };
class Liquid: virtual public Matter { public: float Viscosity; };
Usman Younis
class Solid: virtual public Matter { public: float lattice_constant; };
class Gas: virtual public Matter { public: float pressure; };
The virtual keyword tells the compiler to inherit only one sub object from a class intoinherit only one sub-object from a class into subsequent derived classes, which fixes the ambiguity problem.
In general, you should avoid such multiple
Usman Younis
inheritance; however, if you have considerable experience in C++ you might find reasons to use it in unusual situations.
Containership: Classes within classes
Aggregation: A class has object/objects of other classes l h b b f h l as members
A class aggregates class B, if A has an instance of B
Represents a Weak “has-a” relationship
Usman Younis
Represents a Weak has a relationship Entity can exist with out the composite E.g., An office “has-a” telephone
Composition: A class has object/objects of other classes l h b b f h l as members
Represents a Strong “has-a” relationship Entity cannot exist with out the composite E.g.,
Usman Younis
E.g., A Car “has-an” Engine
Redefining Access Specification
Public members of the base class would have private/protected access in the derived classprivate/protected access in the derived class, depending upon the access specifier used, i.e., :private, or :protected
Usman Younis