SlideShare a Scribd company logo
Object Oriented
Programming using C++
By Mohamed Gamal
© Mohamed Gamal 2024
The topics of today’s lecture:
Agenda
Object Oriented Programming (OOP) using C++ - Lecture 3
#include <iostream>
#include <string>
using namespace std;
class Car {
private:
string make;
double price;
int year;
public:
Car() : make(""), price(0.0), year(0)
{ }
Car(string carMake, double carPrice, int carYear)
{
make = carMake;
price = carPrice;
year = carYear;
}
void setDetails() {
cout << "Enter car make: ";
getline(cin, make);
cout << "Enter car price: ";
cin >> price;
cout << "Enter production year: ";
cin >> year;
}
void displayDetails() const {
cout << "Car Make (Company): " << make << endl;
cout << "Car Price: " << price << endl;
cout << "Car Year: " << year << endl;
}
};
int main() {
Car myCar;
// Get car details
myCar.setDetails();
// Show the car details
cout << "Car Details:n";
myCar.displayDetails();
return 0;
}
Example
Car Class
Object Oriented Programming (OOP) using C++ - Lecture 3
Inheritance in OOP
– Inheritance is probably the most powerful feature of object-oriented
programming, after classes themselves.
– Inheritance is the process of creating new classes, called derived classes, from
existing or base classes.
– The derived class inherits all the capabilities of the base class but can add
embellishments and refinements of its own.
– The main class remain unchanged.
– Inheritance allows code reusability.
Object Oriented Programming (OOP) using C++ - Lecture 3
#include <iostream>
using namespace std;
class Counter //base class
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count(0) //no-arg constructor
{ }
Counter(int c) : count(c) //1-arg constructor
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
class CountDn : public Counter //derived class
{
public:
Counter operator -- () //decr count (prefix)
{
return Counter(--count);
}
};
int main()
{
CountDn c1; //c1 of class CountDn
cout << "c1 = " << c1.get_count() << endl; //display c1
++c1;
++c1;
++c1;
cout << "c1 = " << c1.get_count() << endl; //display it
--c1;
--c1;
cout << "c1 = " << c1.get_count() << endl; //display it
return 0;
}
Example
#include <iostream>
using namespace std;
// Base (Super) Class
class Shape
{
private:
int height;
int width;
public:
Shape() : height(0), width(0)
{ }
Shape(int h, int w) : height(h), width(w)
{ }
int getHeight() const {
return height;
}
int getWidth() const {
return width;
}
void printData() const {
cout << "Height: " << height << endl;
cout << "Width: " << width << endl;
}
};
// Dervied (Sub) Class
class Square : public Shape
{
public:
Square() : Shape()
{ }
Square(int s) : Shape(s, s)
{ }
int getArea() const {
return getHeight() * getWidth();
}
};
// Dervied (Sub) Class
class Rectangle : public Shape
{
public:
Rectangle() : Shape()
{ }
Rectangle(int h, int w) : Shape(h, w)
{ }
int getArea() const {
return getHeight() * getWidth();
}
};
int main()
{
Square s1(5);
Rectangle r1(7, 2);
cout << "Square Area: " << s1.getArea() << endl;
cout << "Rectangle Area: " << r1.getArea() << endl;
return 0;
}
Example
The protected Access Specifier
– A protected member can be accessed by member functions in its own
class or in any class derived from its own class.
– It can’t be accessed from functions outside these classes, such as main().
#include <iostream>
using namespace std;
class Counter
{
protected: //NOTE: not private
unsigned int count; //count
public:
Counter() : count(0) //constructor, no args
{ }
Counter(int c) : count(c) //constructor, one arg
{ }
unsigned int get_count() const //return count
{
return count;
}
Counter operator ++ () //incr count (prefix)
{
return Counter(++count);
}
};
class CountDn : public Counter
{
public:
CountDn() : Counter() //constructor, no args
{ }
CountDn(int c) : Counter(c) //constructor, 1 arg
{ }
CountDn operator -- () //decr count (prefix)
{
return CountDn(--count);
}
};
int main()
{
CountDn c1; //class CountDn
CountDn c2(100);
cout << "c1 = " << c1.get_count() << endl; //display
cout << "c2 = " << c2.get_count() << endl; //display
++c1; ++c1; ++c1; //increment c1
cout << "c1 = " << c1.get_count() << endl; //display it
--c2; --c2; //decrement c2
cout << "c2 = " << c2.get_count() << endl; //display it
CountDn c3 = --c2; //create c3 from c2
cout << "c3 = " << c3.get_count() << endl; //display c3
return 0;
}
Derived Class
Constructors
The CountDn() constructor calls
the Counter() constructor in the
base class.
#include <iostream>
#include <process.h> //for exit()
using namespace std;
class Stack
{
protected: //NOTE: can’t be private
enum { MAX = 3 }; //size of stack array
int st[MAX]; //stack: array of integers
int top; //index to top of stack
public:
Stack() : top(-1) //constructor
{ }
void push(int var) //put number on stack
{
st[++top] = var;
}
int pop() //take number off stack
{
return st[top--];
}
};
class Stack2 : public Stack
{
public:
void push(int var) //put number on stack
{
if (top >= MAX - 1) //error if stack full
{
cout << "nError: stack is full";
exit(1);
}
Stack::push(var); //call push() in Stack class
}
int pop() //take number off stack
{
if (top < 0) //error if stack empty
{
cout << "nError: stack is emptyn";
exit(1);
}
return Stack::pop(); //call pop() in Stack class
}
};
int main()
{
Stack2 s1;
s1.push(11); //push some values onto stack
s1.push(22);
s1.push(33);
cout << endl << s1.pop(); //pop some values from stack
cout << endl << s1.pop();
cout << endl << s1.pop();
cout << endl << s1.pop(); //oops, popped one too many...
return 0;
}
Overriding Member
Functions
#include <iostream>
using namespace std;
enum posneg { pos, neg }; //for sign in DistSign
class Distance
{
protected: //NOTE: can't be private
int feet;
float inches;
public:
Distance() : feet(0), inches(0.0)
{ }
Distance(int ft, float in) : feet(ft), inches(in)
{ }
void getdist() {
cout << "nEnter feet: "; cin >> feet;
cout << "Enter inches: "; cin >> inches;
}
void showdist() const {
cout << feet << "' - " << inches << '"';
}
};
class DistSign : public Distance //adds sign to Distance
{
private:
posneg sign; //sign is pos or neg
public:
DistSign() : Distance() //call base constructor
{
sign = pos; //set the sign to +
}
// 2- or 3-arg constructor
DistSign(int ft, float in, posneg sg = pos) : Distance(ft, in) //call base constructor
{
sign = sg; //set the sign
}
void getdist() //get length from user
{
Distance::getdist(); //call base getdist()
char ch; //get sign from user
cout << "Enter sign (+ or -): ";
cin >> ch;
sign = (ch == '+') ? pos : neg;
}
void showdist() const //display distance
{
cout << ((sign == pos) ? "(+)" : "(-)"); //show sign
Distance::showdist(); //ft and in
}
};
int main()
{
DistSign alpha; //no-arg constructor
alpha.getdist(); //get alpha from user
DistSign beta(11, 6.25); //2-arg constructor
DistSign gamma(100, 5.5, neg); //3-arg constructor
//display all distances
cout << "nalpha = ";
alpha.showdist();
cout << "nbeta = ";
beta.showdist();
cout << "ngamma = ";
gamma.showdist();
return 0;
}
Derived Class
Constructors
More Complex Example
Object Oriented Programming (OOP) using C++ - Lecture 3
Class Hierarchies
UML Class Diagram
#include <iostream>
using namespace std;
const int LEN = 80; //maximum length of names
class employee //employee class
{
private:
char name[LEN]; //employee name
unsigned long number; //employee number
public:
void getdata()
{
cout << "n Enter last name: "; cin >> name;
cout << " Enter number: "; cin >> number;
}
void putdata() const
{
cout << "n Name: " << name;
cout << "n Number: " << number;
}
};
class manager : public employee //management class
{
private:
char title[LEN]; //"vice-president" etc.
double dues; //golf club dues
public:
void getdata()
{
employee::getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
}
void putdata() const
{
employee::putdata();
cout << "n Title: " << title;
cout << "n Golf club dues: " << dues;
}
};
class scientist : public employee //scientist class
{
private:
int pubs; //number of publications
public:
void getdata()
{
employee::getdata();
cout << " Enter number of pubs: "; cin >> pubs;
}
void putdata() const
{
employee::putdata();
cout << "n Number of publications: " << pubs;
}
};
class laborer : public employee //laborer class
{
};
int main()
{
manager m1;
scientist s1;
laborer l1;
//get data for several employees
cout << "nEnter data for manager 1";
m1.getdata();
cout << "nEnter data for scientist 1";
s1.getdata();
cout << "nEnter data for laborer 1";
l1.getdata();
//display data for several employees
cout << "nData on manager 1";
m1.putdata();
cout << "nData on scientist 1";
s1.putdata();
cout << "nData on laborer 1";
l1.putdata();
return 0;
}
Example
Class Hierarchies
Object Oriented Programming (OOP) using C++ - Lecture 3
#include <iostream>
using namespace std;
class A //base class
{
private:
int privdataA;
protected:
int protdataA;
public:
int pubdataA;
};
class B : public A //publicly-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
class C : private A //privately-derived class
{
public:
void funct()
{
int a;
a = privdataA; //error: not accessible
a = protdataA; //OK
a = pubdataA; //OK
}
};
int main()
{
int a;
B objB;
a = objB.privdataA; //error: not accessible
a = objB.protdataA; //error: not accessible
a = objB.pubdataA; //OK (A public to B)
C objC;
a = objC.privdataA; //error: not accessible
a = objC.protdataA; //error: not accessible
a = objC.pubdataA; //error: not accessible (A private to C)
return 0;
}
publicly- and privately-
derived classes
Object Oriented Programming (OOP) using C++ - Lecture 3
Object Oriented Programming (OOP) using C++ - Lecture 3
Multiple Inheritance
UML Class Diagram
class A // base class A
{ };
class B // base class B
{ };
class C : public A, public B // C is
derived from A and B
{ };
#include <iostream>
using namespace std;
const int LEN = 80; //maximum length of names
class student //educational background
{
private:
char school[LEN]; //name of school or university
char degree[LEN]; //highest degree earned
public:
void getedu()
{
cout << " Enter name of school or university: ";
cin >> school;
cout << " Enter highest degree earned (Highschool, Bachelor’s, Master’s, PhD): ";
cin >> degree;
}
void putedu() const
{
cout << "n School or university: " << school;
cout << "n Highest degree earned: " << degree;
}
};
class employee
{
private:
char name[LEN]; //employee name
unsigned long number; //employee number
public:
void getdata()
{
cout << "n Enter last name: "; cin >> name;
cout << " Enter number: "; cin >> number;
}
void putdata() const
{
cout << "n Name: " << name;
cout << "n Number: " << number;
}
};
class manager : private employee, private student //management
{
private:
char title[LEN]; //"vice-president" etc.
double dues; //golf club dues
public:
void getdata()
{
employee::getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
student::getedu();
}
void putdata() const
{
employee::putdata();
cout << "n Title: " << title;
cout << "n Golf club dues: " << dues;
student::putedu();
}
};
class scientist : private employee, private student //scientist
{
private:
int pubs; //number of publications
public:
void getdata()
{
employee::getdata();
cout << " Enter number of pubs: "; cin >> pubs;
student::getedu();
}
void putdata() const
{
employee::putdata();
cout << "n Number of publications: " << pubs;
student::putedu();
}
};
class laborer : public employee //laborer
{ };
int main()
{
manager m1;
scientist s1;
laborer l1;
//get data for several employees
cout << "nEnter data for manager 1";
m1.getdata();
cout << "nEnter data for scientist 1";
s1.getdata();
cout << "nEnter data for laborer 1";
l1.getdata();
//display data for several employees
cout << "nData on manager 1";
m1.putdata();
cout << "nData on scientist 1";
s1.putdata();
cout << "nData on laborer 1";
l1.putdata();
return 0;
}
Example
private Derivation
– The manager and scientist classes in EMPMULT are privately derived from the
employee and student classes.
– There is no need to use public derivation because objects of manager and
scientist never call routines in the employee and student base classes.
– However, the laborer class must be publicly derived from employer, since it
has no member functions of its own and relies on those in employee.
#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string name;
int age;
public:
Person() : name(""), age(0)
{ }
Person(string n, int a) : name(n), age(a)
{ }
void getPerson() //get person from user
{
cout << "tEnter name: "; cin >> name;
cout << "tEnter age: "; cin >> age;
}
void showPerson() const //display type
{
cout << "tName: " << name << endl;
cout << "tAge: " << age << endl;
}
};
class Employee
{
private:
string ID;
double salary;
public:
Employee() : ID(""), salary(0.0)
{ }
Employee(string id, double s) : ID(id), salary(s)
{ }
void getEmployee() //get Employee from user
{
cout << "tEnter ID: "; cin >> ID;
cout << "tEnter salary: "; cin >> salary;
}
void showEmployee() const //display Employee
{
cout << "tID: " << ID << endl;
cout << "tSalary: " << salary << endl;
}
};
class Teacher : public Person, public Employee
{
private:
string rank;
public:
Teacher() : Person(), Employee(), rank("")
{ }
Teacher(string name, int age, string ID, double salary, string r)
: Person(name, age), Employee(ID, salary), rank(r)
{ }
void getTeacher()
{
Person::getPerson();
Employee::getEmployee();
cout << "tEnter rank: "; cin >> rank;
}
void showTeacher() const
{
Person::showPerson();
Employee::showEmployee();
cout << "trank: " << rank << endl;
}
};
int main()
{
Teacher t1;
cout << "Enter Teacher 1 data:nn";
t1.getTeacher();
Teacher t2("Ahmed", 38, "1234", 2500, "Excellent");
//display Teacher data
cout << "nnTeacher 1:n";
t1.showTeacher();
cout << "nnTeacher 2:n";
t2.showTeacher();
return 0;
}
Constructors in Multiple
Inheritance
#include <iostream>
using namespace std;
class A {
public:
void show() { cout << "Class An"; }
};
class B {
public:
void show() { cout << "Class Bn"; }
};
class C : public A, public B
{};
int main() {
C objC; //object of class C
// objC.show(); //ambiguous--will not compile
objC.A::show(); //OK
objC.B::show(); //OK
return 0;
}
Ambiguity in Multiple
Inheritance
1) Two base classes have functions
with the same name, while a class
derived from both base classes
has no function with this name.
How do objects of the derived
class access the correct base class
function?
#include <iostream>
using namespace std;
class A {
public:
void func();
};
class B : public A
{ };
class C : public A
{ };
class D : public B, public C
{ };
int main()
{
D objD;
objD.func(); //ambiguous: won’t compile
return 0;
}
Ambiguity in Multiple
Inheritance
2) Another kind of ambiguity arises
if you derive a class from two
classes that are each derived
from the same class.
This creates a diamond-shaped
inheritance tree.
#include <iostream>
#include <string>
using namespace std;
class student //educational background
{
private:
string school; //name of school or university
string degree; //highest degree earned
public:
void getedu()
{
cout << " Enter name of school or university: ";
cin >> school;
cout << " Enter highest degree earned (Highschool, Bachelor’s, Master’s, PhD): ";
cin >> degree;
}
void putedu() const
{
cout << "n School or university: " << school;
cout << "n Highest degree earned: " << degree;
}
};
class employee
{
private:
string name; //employee name
unsigned long number; //employee number
public:
void getdata()
{
cout << "n Enter last name: "; cin >> name;
cout << " Enter number: "; cin >> number;
}
void putdata() const
{
cout << "n Name: " << name;
cout << "n Number: " << number;
}
};
class manager //management
{
private:
string title; //"vice-president" etc.
double dues; //golf club dues
employee emp; //** object of class employee
student stu; //** object of class student
public:
void getdata()
{
emp.getdata();
cout << " Enter title: "; cin >> title;
cout << " Enter golf club dues: "; cin >> dues;
stu.getedu();
}
void putdata() const
{
emp.putdata();
cout << "n Title: " << title;
cout << "n Golf club dues: " << dues;
stu.putedu();
}
};
class scientist //scientist
{
private:
int pubs; //number of publications
employee emp; //** object of class employee
student stu; //** object of class student
public:
void getdata()
{
emp.getdata();
cout << " Enter number of pubs: "; cin >> pubs;
stu.getedu();
}
void putdata() const
{
emp.putdata();
cout << "n Number of publications: " << pubs;
stu.putedu();
}
};
class laborer //laborer
{
private:
employee emp; //object of class employee
public:
void getdata() {
emp.getdata();
}
void putdata() const {
emp.putdata();
}
};
int main()
{
manager m1;
scientist s1;
laborer l1;
//get data for several employees
cout << "nEnter data for manager 1";
m1.getdata();
cout << "nEnter data for scientist 1";
s1.getdata();
cout << "nEnter data for laborer 1";
l1.getdata();
//display data for several employees
cout << "nData on manager 1";
m1.putdata();
cout << "nData on scientist 1";
s1.putdata();
cout << "nData on laborer 1";
l1.putdata();
return 0;
}
Aggregation instead of
inheritance
2) Another kind of ambiguity arises
if you derive a class from two
classes that are each derived
from the same class.
This creates a diamond-shaped
inheritance tree.
End of lecture 3
ThankYou!
Ad

More Related Content

Similar to Object Oriented Programming (OOP) using C++ - Lecture 3 (20)

polymorphism in c++ with Full Explanation.
polymorphism in c++ with Full Explanation.polymorphism in c++ with Full Explanation.
polymorphism in c++ with Full Explanation.
UdayGumre
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
Mitul Patel
 
C++ programs
C++ programsC++ programs
C++ programs
Mukund Gandrakota
 
Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4Object Oriented Programming (OOP) using C++ - Lecture 4
Object Oriented Programming (OOP) using C++ - Lecture 4
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
yash production
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
SushmaGavaraskar
 
P1
P1P1
P1
Hitesh Wagle
 
Pointers
PointersPointers
Pointers
Hitesh Wagle
 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
aksachdevahosymills
 
Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
C++ Language
C++ LanguageC++ Language
C++ Language
Vidyacenter
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
Mohammad Shaker
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
khasmanjalali
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
Virtual inheritance
Virtual inheritanceVirtual inheritance
Virtual inheritance
Rajendran Praj
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
vikram mahendra
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
Anwar Ul Haq
 
polymorphism in c++ with Full Explanation.
polymorphism in c++ with Full Explanation.polymorphism in c++ with Full Explanation.
polymorphism in c++ with Full Explanation.
UdayGumre
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
Mitul Patel
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
yash production
 
Can you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdfCan you finish and write the int main for the code according to the in.pdf
Can you finish and write the int main for the code according to the in.pdf
aksachdevahosymills
 
C++ Language
C++ LanguageC++ Language
C++ Language
Vidyacenter
 
C++ L11-Polymorphism
C++ L11-PolymorphismC++ L11-Polymorphism
C++ L11-Polymorphism
Mohammad Shaker
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
vandna123
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
khasmanjalali
 
The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196The Ring programming language version 1.7 book - Part 87 of 196
The Ring programming language version 1.7 book - Part 87 of 196
Mahmoud Samir Fayed
 
Virtual inheritance
Virtual inheritanceVirtual inheritance
Virtual inheritance
Rajendran Praj
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
Dendi Riadi
 
project report in C++ programming and SQL
project report in C++ programming and SQLproject report in C++ programming and SQL
project report in C++ programming and SQL
vikram mahendra
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
Anwar Ul Haq
 

More from Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt (20)

How to install CS50 Library (Step-by-step guide)
How to install CS50 Library (Step-by-step guide)How to install CS50 Library (Step-by-step guide)
How to install CS50 Library (Step-by-step guide)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding Singular Value Decomposition (SVD)
Understanding Singular Value Decomposition (SVD)Understanding Singular Value Decomposition (SVD)
Understanding Singular Value Decomposition (SVD)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Linux OS (Part 2) - Tutorial
Introduction to Linux OS (Part 2) - TutorialIntroduction to Linux OS (Part 2) - Tutorial
Introduction to Linux OS (Part 2) - Tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Linux OS (Part 1) - Tutorial
Introduction to Linux OS (Part 1) - TutorialIntroduction to Linux OS (Part 1) - Tutorial
Introduction to Linux OS (Part 1) - Tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
How to use tmux in Linux - A basic tutorial
How to use tmux in Linux - A basic tutorialHow to use tmux in Linux - A basic tutorial
How to use tmux in Linux - A basic tutorial
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Getting started with neural networks (NNs)
Getting started with neural networks (NNs)Getting started with neural networks (NNs)
Getting started with neural networks (NNs)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding K-Nearest Neighbor (KNN) Algorithm
Understanding K-Nearest Neighbor (KNN) AlgorithmUnderstanding K-Nearest Neighbor (KNN) Algorithm
Understanding K-Nearest Neighbor (KNN) Algorithm
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Understanding Convolutional Neural Networks (CNN)
Understanding Convolutional Neural Networks (CNN)Understanding Convolutional Neural Networks (CNN)
Understanding Convolutional Neural Networks (CNN)
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Luhn's algorithm to validate Egyptian ID numbers
Luhn's algorithm to validate Egyptian ID numbersLuhn's algorithm to validate Egyptian ID numbers
Luhn's algorithm to validate Egyptian ID numbers
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Difference between Mean and Weighted Mean
Difference between Mean and Weighted MeanDifference between Mean and Weighted Mean
Difference between Mean and Weighted Mean
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Complier Design - Operations on Languages, RE, Finite Automata
Complier Design - Operations on Languages, RE, Finite AutomataComplier Design - Operations on Languages, RE, Finite Automata
Complier Design - Operations on Languages, RE, Finite Automata
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5Object Oriented Programming (OOP) using C++ - Lecture 5
Object Oriented Programming (OOP) using C++ - Lecture 5
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1Object Oriented Programming (OOP) using C++ - Lecture 1
Object Oriented Programming (OOP) using C++ - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 2
Introduction to Operating System - Lecture 2Introduction to Operating System - Lecture 2
Introduction to Operating System - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 1
Introduction to Operating System - Lecture 1Introduction to Operating System - Lecture 1
Introduction to Operating System - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Operating System - Lecture 3
Introduction to Operating System - Lecture 3Introduction to Operating System - Lecture 3
Introduction to Operating System - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Freelancing - Quick Guide
Introduction to Freelancing - Quick GuideIntroduction to Freelancing - Quick Guide
Introduction to Freelancing - Quick Guide
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Python Prog. - Lecture 1
Introduction to Python Prog. - Lecture 1Introduction to Python Prog. - Lecture 1
Introduction to Python Prog. - Lecture 1
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Python Prog. - Lecture 3
Introduction to Python Prog. - Lecture 3Introduction to Python Prog. - Lecture 3
Introduction to Python Prog. - Lecture 3
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Introduction to Python Prog. - Lecture 2
Introduction to Python Prog. - Lecture 2Introduction to Python Prog. - Lecture 2
Introduction to Python Prog. - Lecture 2
Faculty of Computers and Informatics, Suez Canal University, Ismailia, Egypt
 
Ad

Recently uploaded (20)

Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Implementing promises with typescripts, step by step
Implementing promises with typescripts, step by stepImplementing promises with typescripts, step by step
Implementing promises with typescripts, step by step
Ran Wahle
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Creating Automated Tests with AI - Cory House - Applitools.pdf
Creating Automated Tests with AI - Cory House - Applitools.pdfCreating Automated Tests with AI - Cory House - Applitools.pdf
Creating Automated Tests with AI - Cory House - Applitools.pdf
Applitools
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Odoo ERP for Education Management to Streamline Your Education Process
Odoo ERP for Education Management to Streamline Your Education ProcessOdoo ERP for Education Management to Streamline Your Education Process
Odoo ERP for Education Management to Streamline Your Education Process
iVenture Team LLP
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Download YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full ActivatedDownload YouTube By Click 2025 Free Full Activated
Download YouTube By Click 2025 Free Full Activated
saniamalik72555
 
Exploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the FutureExploring Wayland: A Modern Display Server for the Future
Exploring Wayland: A Modern Display Server for the Future
ICS
 
Expand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchangeExpand your AI adoption with AgentExchange
Expand your AI adoption with AgentExchange
Fexle Services Pvt. Ltd.
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
Societal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainabilitySocietal challenges of AI: biases, multilinguism and sustainability
Societal challenges of AI: biases, multilinguism and sustainability
Jordi Cabot
 
Implementing promises with typescripts, step by step
Implementing promises with typescripts, step by stepImplementing promises with typescripts, step by step
Implementing promises with typescripts, step by step
Ran Wahle
 
Innovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at allInnovative Approaches to Software Dev no good at all
Innovative Approaches to Software Dev no good at all
ayeshakanwal75
 
Creating Automated Tests with AI - Cory House - Applitools.pdf
Creating Automated Tests with AI - Cory House - Applitools.pdfCreating Automated Tests with AI - Cory House - Applitools.pdf
Creating Automated Tests with AI - Cory House - Applitools.pdf
Applitools
 
Microsoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptxMicrosoft Excel Core Points Training.pptx
Microsoft Excel Core Points Training.pptx
Mekonnen
 
DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025DVDFab Crack FREE Download Latest Version 2025
DVDFab Crack FREE Download Latest Version 2025
younisnoman75
 
How can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptxHow can one start with crypto wallet development.pptx
How can one start with crypto wallet development.pptx
laravinson24
 
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Requirements in Engineering AI- Enabled Systems: Open Problems and Safe AI Sy...
Lionel Briand
 
Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)Who Watches the Watchmen (SciFiDevCon 2025)
Who Watches the Watchmen (SciFiDevCon 2025)
Allon Mureinik
 
Odoo ERP for Education Management to Streamline Your Education Process
Odoo ERP for Education Management to Streamline Your Education ProcessOdoo ERP for Education Management to Streamline Your Education Process
Odoo ERP for Education Management to Streamline Your Education Process
iVenture Team LLP
 
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage DashboardsAdobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
Adobe Marketo Engage Champion Deep Dive - SFDC CRM Synch V2 & Usage Dashboards
BradBedford3
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Tools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google CertificateTools of the Trade: Linux and SQL - Google Certificate
Tools of the Trade: Linux and SQL - Google Certificate
VICTOR MAESTRE RAMIREZ
 
Ad

Object Oriented Programming (OOP) using C++ - Lecture 3

  • 1. Object Oriented Programming using C++ By Mohamed Gamal © Mohamed Gamal 2024
  • 2. The topics of today’s lecture: Agenda
  • 4. #include <iostream> #include <string> using namespace std; class Car { private: string make; double price; int year; public: Car() : make(""), price(0.0), year(0) { } Car(string carMake, double carPrice, int carYear) { make = carMake; price = carPrice; year = carYear; } void setDetails() { cout << "Enter car make: "; getline(cin, make); cout << "Enter car price: "; cin >> price; cout << "Enter production year: "; cin >> year; } void displayDetails() const { cout << "Car Make (Company): " << make << endl; cout << "Car Price: " << price << endl; cout << "Car Year: " << year << endl; } }; int main() { Car myCar; // Get car details myCar.setDetails(); // Show the car details cout << "Car Details:n"; myCar.displayDetails(); return 0; } Example Car Class
  • 6. Inheritance in OOP – Inheritance is probably the most powerful feature of object-oriented programming, after classes themselves. – Inheritance is the process of creating new classes, called derived classes, from existing or base classes. – The derived class inherits all the capabilities of the base class but can add embellishments and refinements of its own. – The main class remain unchanged. – Inheritance allows code reusability.
  • 8. #include <iostream> using namespace std; class Counter //base class { protected: //NOTE: not private unsigned int count; //count public: Counter() : count(0) //no-arg constructor { } Counter(int c) : count(c) //1-arg constructor { } unsigned int get_count() const //return count { return count; } Counter operator ++ () //incr count (prefix) { return Counter(++count); } }; class CountDn : public Counter //derived class { public: Counter operator -- () //decr count (prefix) { return Counter(--count); } }; int main() { CountDn c1; //c1 of class CountDn cout << "c1 = " << c1.get_count() << endl; //display c1 ++c1; ++c1; ++c1; cout << "c1 = " << c1.get_count() << endl; //display it --c1; --c1; cout << "c1 = " << c1.get_count() << endl; //display it return 0; } Example
  • 9. #include <iostream> using namespace std; // Base (Super) Class class Shape { private: int height; int width; public: Shape() : height(0), width(0) { } Shape(int h, int w) : height(h), width(w) { } int getHeight() const { return height; } int getWidth() const { return width; } void printData() const { cout << "Height: " << height << endl; cout << "Width: " << width << endl; } }; // Dervied (Sub) Class class Square : public Shape { public: Square() : Shape() { } Square(int s) : Shape(s, s) { } int getArea() const { return getHeight() * getWidth(); } }; // Dervied (Sub) Class class Rectangle : public Shape { public: Rectangle() : Shape() { } Rectangle(int h, int w) : Shape(h, w) { } int getArea() const { return getHeight() * getWidth(); } }; int main() { Square s1(5); Rectangle r1(7, 2); cout << "Square Area: " << s1.getArea() << endl; cout << "Rectangle Area: " << r1.getArea() << endl; return 0; } Example
  • 10. The protected Access Specifier – A protected member can be accessed by member functions in its own class or in any class derived from its own class. – It can’t be accessed from functions outside these classes, such as main().
  • 11. #include <iostream> using namespace std; class Counter { protected: //NOTE: not private unsigned int count; //count public: Counter() : count(0) //constructor, no args { } Counter(int c) : count(c) //constructor, one arg { } unsigned int get_count() const //return count { return count; } Counter operator ++ () //incr count (prefix) { return Counter(++count); } }; class CountDn : public Counter { public: CountDn() : Counter() //constructor, no args { } CountDn(int c) : Counter(c) //constructor, 1 arg { } CountDn operator -- () //decr count (prefix) { return CountDn(--count); } }; int main() { CountDn c1; //class CountDn CountDn c2(100); cout << "c1 = " << c1.get_count() << endl; //display cout << "c2 = " << c2.get_count() << endl; //display ++c1; ++c1; ++c1; //increment c1 cout << "c1 = " << c1.get_count() << endl; //display it --c2; --c2; //decrement c2 cout << "c2 = " << c2.get_count() << endl; //display it CountDn c3 = --c2; //create c3 from c2 cout << "c3 = " << c3.get_count() << endl; //display c3 return 0; } Derived Class Constructors The CountDn() constructor calls the Counter() constructor in the base class.
  • 12. #include <iostream> #include <process.h> //for exit() using namespace std; class Stack { protected: //NOTE: can’t be private enum { MAX = 3 }; //size of stack array int st[MAX]; //stack: array of integers int top; //index to top of stack public: Stack() : top(-1) //constructor { } void push(int var) //put number on stack { st[++top] = var; } int pop() //take number off stack { return st[top--]; } }; class Stack2 : public Stack { public: void push(int var) //put number on stack { if (top >= MAX - 1) //error if stack full { cout << "nError: stack is full"; exit(1); } Stack::push(var); //call push() in Stack class } int pop() //take number off stack { if (top < 0) //error if stack empty { cout << "nError: stack is emptyn"; exit(1); } return Stack::pop(); //call pop() in Stack class } }; int main() { Stack2 s1; s1.push(11); //push some values onto stack s1.push(22); s1.push(33); cout << endl << s1.pop(); //pop some values from stack cout << endl << s1.pop(); cout << endl << s1.pop(); cout << endl << s1.pop(); //oops, popped one too many... return 0; } Overriding Member Functions
  • 13. #include <iostream> using namespace std; enum posneg { pos, neg }; //for sign in DistSign class Distance { protected: //NOTE: can't be private int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) { } void getdist() { cout << "nEnter feet: "; cin >> feet; cout << "Enter inches: "; cin >> inches; } void showdist() const { cout << feet << "' - " << inches << '"'; } }; class DistSign : public Distance //adds sign to Distance { private: posneg sign; //sign is pos or neg public: DistSign() : Distance() //call base constructor { sign = pos; //set the sign to + } // 2- or 3-arg constructor DistSign(int ft, float in, posneg sg = pos) : Distance(ft, in) //call base constructor { sign = sg; //set the sign } void getdist() //get length from user { Distance::getdist(); //call base getdist() char ch; //get sign from user cout << "Enter sign (+ or -): "; cin >> ch; sign = (ch == '+') ? pos : neg; } void showdist() const //display distance { cout << ((sign == pos) ? "(+)" : "(-)"); //show sign Distance::showdist(); //ft and in } }; int main() { DistSign alpha; //no-arg constructor alpha.getdist(); //get alpha from user DistSign beta(11, 6.25); //2-arg constructor DistSign gamma(100, 5.5, neg); //3-arg constructor //display all distances cout << "nalpha = "; alpha.showdist(); cout << "nbeta = "; beta.showdist(); cout << "ngamma = "; gamma.showdist(); return 0; } Derived Class Constructors More Complex Example
  • 16. #include <iostream> using namespace std; const int LEN = 80; //maximum length of names class employee //employee class { private: char name[LEN]; //employee name unsigned long number; //employee number public: void getdata() { cout << "n Enter last name: "; cin >> name; cout << " Enter number: "; cin >> number; } void putdata() const { cout << "n Name: " << name; cout << "n Number: " << number; } }; class manager : public employee //management class { private: char title[LEN]; //"vice-president" etc. double dues; //golf club dues public: void getdata() { employee::getdata(); cout << " Enter title: "; cin >> title; cout << " Enter golf club dues: "; cin >> dues; } void putdata() const { employee::putdata(); cout << "n Title: " << title; cout << "n Golf club dues: " << dues; } }; class scientist : public employee //scientist class { private: int pubs; //number of publications public: void getdata() { employee::getdata(); cout << " Enter number of pubs: "; cin >> pubs; } void putdata() const { employee::putdata(); cout << "n Number of publications: " << pubs; } }; class laborer : public employee //laborer class { }; int main() { manager m1; scientist s1; laborer l1; //get data for several employees cout << "nEnter data for manager 1"; m1.getdata(); cout << "nEnter data for scientist 1"; s1.getdata(); cout << "nEnter data for laborer 1"; l1.getdata(); //display data for several employees cout << "nData on manager 1"; m1.putdata(); cout << "nData on scientist 1"; s1.putdata(); cout << "nData on laborer 1"; l1.putdata(); return 0; } Example Class Hierarchies
  • 18. #include <iostream> using namespace std; class A //base class { private: int privdataA; protected: int protdataA; public: int pubdataA; }; class B : public A //publicly-derived class { public: void funct() { int a; a = privdataA; //error: not accessible a = protdataA; //OK a = pubdataA; //OK } }; class C : private A //privately-derived class { public: void funct() { int a; a = privdataA; //error: not accessible a = protdataA; //OK a = pubdataA; //OK } }; int main() { int a; B objB; a = objB.privdataA; //error: not accessible a = objB.protdataA; //error: not accessible a = objB.pubdataA; //OK (A public to B) C objC; a = objC.privdataA; //error: not accessible a = objC.protdataA; //error: not accessible a = objC.pubdataA; //error: not accessible (A private to C) return 0; } publicly- and privately- derived classes
  • 21. Multiple Inheritance UML Class Diagram class A // base class A { }; class B // base class B { }; class C : public A, public B // C is derived from A and B { };
  • 22. #include <iostream> using namespace std; const int LEN = 80; //maximum length of names class student //educational background { private: char school[LEN]; //name of school or university char degree[LEN]; //highest degree earned public: void getedu() { cout << " Enter name of school or university: "; cin >> school; cout << " Enter highest degree earned (Highschool, Bachelor’s, Master’s, PhD): "; cin >> degree; } void putedu() const { cout << "n School or university: " << school; cout << "n Highest degree earned: " << degree; } }; class employee { private: char name[LEN]; //employee name unsigned long number; //employee number public: void getdata() { cout << "n Enter last name: "; cin >> name; cout << " Enter number: "; cin >> number; } void putdata() const { cout << "n Name: " << name; cout << "n Number: " << number; } }; class manager : private employee, private student //management { private: char title[LEN]; //"vice-president" etc. double dues; //golf club dues public: void getdata() { employee::getdata(); cout << " Enter title: "; cin >> title; cout << " Enter golf club dues: "; cin >> dues; student::getedu(); } void putdata() const { employee::putdata(); cout << "n Title: " << title; cout << "n Golf club dues: " << dues; student::putedu(); } }; class scientist : private employee, private student //scientist { private: int pubs; //number of publications public: void getdata() { employee::getdata(); cout << " Enter number of pubs: "; cin >> pubs; student::getedu(); } void putdata() const { employee::putdata(); cout << "n Number of publications: " << pubs; student::putedu(); } }; class laborer : public employee //laborer { }; int main() { manager m1; scientist s1; laborer l1; //get data for several employees cout << "nEnter data for manager 1"; m1.getdata(); cout << "nEnter data for scientist 1"; s1.getdata(); cout << "nEnter data for laborer 1"; l1.getdata(); //display data for several employees cout << "nData on manager 1"; m1.putdata(); cout << "nData on scientist 1"; s1.putdata(); cout << "nData on laborer 1"; l1.putdata(); return 0; } Example
  • 23. private Derivation – The manager and scientist classes in EMPMULT are privately derived from the employee and student classes. – There is no need to use public derivation because objects of manager and scientist never call routines in the employee and student base classes. – However, the laborer class must be publicly derived from employer, since it has no member functions of its own and relies on those in employee.
  • 24. #include <iostream> #include <string> using namespace std; class Person { private: string name; int age; public: Person() : name(""), age(0) { } Person(string n, int a) : name(n), age(a) { } void getPerson() //get person from user { cout << "tEnter name: "; cin >> name; cout << "tEnter age: "; cin >> age; } void showPerson() const //display type { cout << "tName: " << name << endl; cout << "tAge: " << age << endl; } }; class Employee { private: string ID; double salary; public: Employee() : ID(""), salary(0.0) { } Employee(string id, double s) : ID(id), salary(s) { } void getEmployee() //get Employee from user { cout << "tEnter ID: "; cin >> ID; cout << "tEnter salary: "; cin >> salary; } void showEmployee() const //display Employee { cout << "tID: " << ID << endl; cout << "tSalary: " << salary << endl; } }; class Teacher : public Person, public Employee { private: string rank; public: Teacher() : Person(), Employee(), rank("") { } Teacher(string name, int age, string ID, double salary, string r) : Person(name, age), Employee(ID, salary), rank(r) { } void getTeacher() { Person::getPerson(); Employee::getEmployee(); cout << "tEnter rank: "; cin >> rank; } void showTeacher() const { Person::showPerson(); Employee::showEmployee(); cout << "trank: " << rank << endl; } }; int main() { Teacher t1; cout << "Enter Teacher 1 data:nn"; t1.getTeacher(); Teacher t2("Ahmed", 38, "1234", 2500, "Excellent"); //display Teacher data cout << "nnTeacher 1:n"; t1.showTeacher(); cout << "nnTeacher 2:n"; t2.showTeacher(); return 0; } Constructors in Multiple Inheritance
  • 25. #include <iostream> using namespace std; class A { public: void show() { cout << "Class An"; } }; class B { public: void show() { cout << "Class Bn"; } }; class C : public A, public B {}; int main() { C objC; //object of class C // objC.show(); //ambiguous--will not compile objC.A::show(); //OK objC.B::show(); //OK return 0; } Ambiguity in Multiple Inheritance 1) Two base classes have functions with the same name, while a class derived from both base classes has no function with this name. How do objects of the derived class access the correct base class function?
  • 26. #include <iostream> using namespace std; class A { public: void func(); }; class B : public A { }; class C : public A { }; class D : public B, public C { }; int main() { D objD; objD.func(); //ambiguous: won’t compile return 0; } Ambiguity in Multiple Inheritance 2) Another kind of ambiguity arises if you derive a class from two classes that are each derived from the same class. This creates a diamond-shaped inheritance tree.
  • 27. #include <iostream> #include <string> using namespace std; class student //educational background { private: string school; //name of school or university string degree; //highest degree earned public: void getedu() { cout << " Enter name of school or university: "; cin >> school; cout << " Enter highest degree earned (Highschool, Bachelor’s, Master’s, PhD): "; cin >> degree; } void putedu() const { cout << "n School or university: " << school; cout << "n Highest degree earned: " << degree; } }; class employee { private: string name; //employee name unsigned long number; //employee number public: void getdata() { cout << "n Enter last name: "; cin >> name; cout << " Enter number: "; cin >> number; } void putdata() const { cout << "n Name: " << name; cout << "n Number: " << number; } }; class manager //management { private: string title; //"vice-president" etc. double dues; //golf club dues employee emp; //** object of class employee student stu; //** object of class student public: void getdata() { emp.getdata(); cout << " Enter title: "; cin >> title; cout << " Enter golf club dues: "; cin >> dues; stu.getedu(); } void putdata() const { emp.putdata(); cout << "n Title: " << title; cout << "n Golf club dues: " << dues; stu.putedu(); } }; class scientist //scientist { private: int pubs; //number of publications employee emp; //** object of class employee student stu; //** object of class student public: void getdata() { emp.getdata(); cout << " Enter number of pubs: "; cin >> pubs; stu.getedu(); } void putdata() const { emp.putdata(); cout << "n Number of publications: " << pubs; stu.putedu(); } }; class laborer //laborer { private: employee emp; //object of class employee public: void getdata() { emp.getdata(); } void putdata() const { emp.putdata(); } }; int main() { manager m1; scientist s1; laborer l1; //get data for several employees cout << "nEnter data for manager 1"; m1.getdata(); cout << "nEnter data for scientist 1"; s1.getdata(); cout << "nEnter data for laborer 1"; l1.getdata(); //display data for several employees cout << "nData on manager 1"; m1.putdata(); cout << "nData on scientist 1"; s1.putdata(); cout << "nData on laborer 1"; l1.putdata(); return 0; } Aggregation instead of inheritance 2) Another kind of ambiguity arises if you derive a class from two classes that are each derived from the same class. This creates a diamond-shaped inheritance tree.
  • 28. End of lecture 3 ThankYou!