Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Class Collection - Object Oriented Programming - Lecture Slides, Slides of Object Oriented Programming

Class Collection, Class Set, Private Collection, Specialization Restriction, Derived class, Base class, Private and protected inheritance, Member functions, Friend functions are points you can learn in this Object Oriented Programming lecture.

Typology: Slides

2011/2012

Uploaded on 11/09/2012

bacha
bacha 🇮🇳

4.3

(42)

214 documents

1 / 29

Toggle sidebar

Related documents


Partial preview of the text

Download Class Collection - Object Oriented Programming - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity! Object-Oriented Programming (OOP) Lecture No. 27 Docsity.com Class Collection class Collection { ... public: void AddElement(int); bool SearchElement(int); bool SearchElementAgain(int); bool DeleteElement(int); }; Docsity.com Specialization (Restriction) • Specialization (Restriction) can be implemented using private and protected inheritance Docsity.com Example – Specialization (Restriction) Person age : [0..125] Adult age : [18..125] setAge( a ) setAge( a ) age = a If age < 18 then error else age = a Docsity.com Example class Person{ … protected: int age; public: bool SetAge(int _age){ if (_age >=0 && _age <= 125) { age = _age; return true; } return false; } }; Docsity.com Example class Parent{ }; class Child : private Parent{ }; int main(){ Child cobj; Parent *pptr = & cobj; //Error return 0; } Docsity.com Example void DoSomething(const Parent &); Child::Child(){ Parent & pPtr = static_cast<Parent &>(*this); DoSomething(pPtr); // DoSomething(*this); } Docsity.com Private Inheritance • The child class object has an anonymous object of parent class object • The default constructor and copy constructor of parent class are called when needed Docsity.com Example int main() { Child cobj1; Child cobj2 = cobj1; //Child cobj2(cobj1); return 0; } Docsity.com Example • Output: Parent Constructor Child Constructor Parent Copy Constructor Child Copy Constructor Docsity.com Private Inheritance • The base class that is more then one level down the hierarchy cannot access the member function of parent class, if we are using private inheritance Docsity.com Private Inheritance • The base class that is more then one level down the hierarchy cannot convert the pointer or reference to child object to that of parent, if we are using private inheritance Docsity.com Class Hierarchy void DoSomething(GrandParent&); class GrandParent{ }; class Parent: private GrandParent{ public: Parent() {DoSomething(*this);} }; Docsity.com Example class Child: private Parent { public: Child() { DoSomething(*this); //Error } }; Docsity.com Class Hierarchy class GrandParent{ public : void DoSomething(); }; class Parent: protected GrandParent{ void SomeFunction(){ DoSomething(); } }; Docsity.com Example class Child: protected Parent { public: Child() { DoSomething(); } }; Docsity.com Protected Inheritance • If B is a protected base and D is derived class then only friends and members of D and friends and members of class derived from D can convert D* to B* or D& to B& Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved