Download presentation
Presentation is loading. Please wait.
Published byCrystal Wilkerson Modified over 6 years ago
1
Presented By: Nazia Hossain Lecturer, Stamford University
Lab 4: Inheritence Presented By: Nazia Hossain Lecturer, Stamford University
2
Inheritence One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class, that provides an opportunity to reuse the code functionality and fast implementation time. When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
3
Base & Derived Classes To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form: class derived-class: access-specifier base-class Where access-specifier is one of public, protected, or private, and base-class is the name of a previously defined class. If the access-specifier is not used, then it is private by default.
4
Consider a base class Shape and its derived class Rectangle as follows:
#include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int w) width = w; } void setHeight(int h) height = h; protected: int width; int height; };
5
// Derived class class Rectangle: public Shape { public: int getArea() return (width * height); } }; int main(void) Rectangle Rect; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; What is the output of this program? Ans:
6
Access Control and Inheritance:
Public Protected Private Same Class Yes Derived Class No Outside Class
7
A derived class inherits all base class methods with the following exceptions:
Constructors, destructors and copy constructors of the base class. Overloaded operators of the base class. The friend functions of the base class.
8
Type of Inheritance: Derived Class Visibility Base Class visibility
Public derivation Private derivation Protected derivation Private Not Inherited Not inherited Protected Public
9
Multiple Inheritances
A C++ class can inherit members from more than one class and here is the extended syntax: class derived-class: access baseA, access baseB....
10
Example of Multiple Inheritance
#include <iostream> using namespace std; // Base class Shape class Shape { public: void setWidth(int w) width = w; } void setHeight(int h) height = h; protected: int width; int height; };
11
// Base class PaintCost class PaintCost { public: int getCost(int area) return area * 70; } }; // Derived class class Rectangle: public Shape, public PaintCost int getArea() return (width * height);
12
int main(void) { Rectangle Rect; int area; Rect. setWidth(5); Rect
int main(void) { Rectangle Rect; int area; Rect.setWidth(5); Rect.setHeight(7); area = Rect.getArea(); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; // Print the total cost of painting cout << "Total paint cost: $" << Rect.getCost(area) << endl; return 0; } What is the output of this program? Answer:
13
Hybrid Inheritance Hybrid Inheritance is the combination of two or more inheritances : single, multiple,multilevel or hierarchical Inheritances. The following is a C++ Program to for Calculating the marks secured by a student.A Parent class with student identification is created and another class called marks is inherited from the main class.This class marks is further inherited by another class called sports and finally the sports class is inherited by the percentage class to calculated the percentage of marks.
14
#include<iostream> #include<conio. h> #include<stdio
#include<iostream> #include<conio.h> #include<stdio.h> #include<string.h> using namespace std; class student_id { //c-madeeasy.blogspot.com int rno; char name[20]; public: void read_id() { cout<<"\n\nEnter the Name of the Student : "; gets(name); cout<<"\n\nEnter the Roll No. : "; cin>>rno; } void display_nr() cout<<"\n\n\t\tSTUDENT REPORT\n\nNAME : "; puts(name); cout<<"\n\nROLL NO. : "<<rno; };
15
class marks:public student_id { public: int i,mark[3]; void read_m() read_id(); for(i=0;i<3;i++) { cout<<"\n\nEnter the Marks Secured in SUBJECT "<<i+1<<" out of 100 : "; cin>>mark[i]; } void display_m() { display_nr(); cout<<"\n\n\tMarks Secured "; for(i=0;i<3;i++) cout<<"\n\nSUBJECT "<<i+1<<" : "<<mark[i]; };
16
class sports { public: int sm; void read_sportm() { cout<<"\n\nEnter the marks in SPORTS out of 10 : "; cin>>sm; } };
17
class percentage:public marks,public sports { public: float total,prcntge; void calculate() read_m(); read_sportm(); total=0; for(i=0;i<3;i++) { total+=mark[i]; } total+=sm; prcntge=(total/310)*100; void display_totp() display_m(); cout<<"\n\nTOTAL = "<<total; cout<<"\n\nPERCENTAGE = "<<prcntge; };
18
int main() { int cont; percentage pc; do pc. calculate(); pc
int main() { int cont; percentage pc; do pc.calculate(); pc.display_totp(); cout<<"\n\nDo You Want to Continue?(1-YES/0-NO)"; cin>>cont; }while(cont==1); getch(); return 0; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.