inheritance - bphanikrishna.files.wordpress.com 23, 2016 · write a c++ program for finding...

22
Inheritance 23-01-2016

Upload: leminh

Post on 21-Mar-2018

216 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Inheritance23-01-2016

Page 2: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Inheritance

Inheritance is the capability of one class to acquire properties andcharacteristics from another class.

For using Inheritance concept in our program we must use at least twoclasses.

The class whose properties are inherited by other class is calledthe Parent or Base or Super class.

And, the class which inherits properties of other class iscalled Child or Derived or Sub class.

Page 3: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Syntax:

Class subclass : < access specifier (public, protected, private) > Superclass.

class Person

{

... .. ...

};

class MathsTeacher : public Person

{

... .. ...

};

Page 4: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Access specifiers in Inheritance

If access specifier is Public

i.e., Class subclass: public Superclass

Then the protected member of super class becomes protected members of sub class and public becomes public

Page 5: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Access specifiers in Inheritance (Continuation)

If access specifier is protected

i.e., Class subclass: protected Superclass

Then the protected and public members of super class becomes protected members of sub class.

Page 6: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Access specifiers in Inheritance (Continuation)

If access specifier is private

i.e., Class subclass: private Superclass

Then the protected and public members of super class becomes private members of sub class.

Page 7: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Simple Inheritance program:#include <iostream>

using namespace std;

// Base class

class Shape {

protected:

int width;

int height;

public:

void setWidth(int w) {

width = w;

}

void setHeight(int h) {

height = h;

}

};

class Rectangle: public Shape {

public:

int getArea() {

return (width * height);

}

};

int main(void) {

Rectangle Rect;

Rect.setWidth(2);

Rect.setHeight(2);

cout << "Total area: " << Rect.getArea() <<

endl;

return 0;

}

Page 8: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Advantages of Inheritance

• Inheritance makes the code reusable. When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.

• Fast implementation time

Page 9: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Types of Inheritance:

In C++, we have 5 different types of Inheritance. Namely,

1. Single Inheritance

2. Multiple Inheritance

3. Hierarchical Inheritance

4. Multilevel Inheritance

5. Hybrid Inheritance (also known as Virtual Inheritance)

Page 10: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Single Inheritance

Single Inheritance: In this type of inheritance one derived class inheritsfrom only one base class. It is the simplest form of Inheritance.

Syntax: class derived-class: access-specifier Base-class

Class B: public A{

};

Page 11: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Multiple Inheritance:

• Multiple Inheritance: In this type of inheritance a single derived class may inherit from two or more than two base classes.

Syntax:

class derived-class: access-specifier baseA, access-specifier baseB....

Class c: public A, public B{

};

Page 12: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Example for Multiple Inheritance:#include <iostream>

using namespace std;

class Shape {

protected:

int width;

int height;

public:

void setWidth(int w) {

width = w;

}

void setHeight(int h) {

height = h;

}

};

class PaintCost {

public:

int getCost(int area) {

int cost;

cout<<"Enter the paint cost for per inches \n";

cin>>cost;

return area * cost;

}

};

class Rectangle: public Shape, public PaintCost {

public:

int getArea() {

return (width * height);

}

};

int main(void) {

Rectangle Rect;

int area;

int w,h;

cout<<"Enter the Widhth and Height \n";

cin>>w>>h;

Rect.setWidth(w);

Rect.setHeight(h);

area = Rect.getArea();

cout << "Total area: " << Rect.getArea() << endl;

cout << "Total paint cost: $" << Rect.getCost(area) << endl;

return 0;

}

Page 13: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Hierarchical Inheritance:

Hierarchical Inheritance: In this type of inheritance, multiple derived classes inherits from a single base class

class A { //content of base class i.e., A };

class B :public A { //content of derived class i.e., B };

class C :public A { //content of derived class i.e., C };

class D :public A { //content of derived class i.e., D };

Page 14: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Example for Hierarchical Inheritance:#include<iostream>

using namespace std;

class arithmetic{

protected:

int op1,op2;

public:

void getop(int a, int b){

op1=a;

op2=b;

}

};

class sum: public arithmetic{

public:

int addition(){

return op1+op2;

}

};

class mul: public arithmetic{

public:

int multiplication(){

return op1*op2;

}

};

int main(){

sum s1;

s1.getop(3,4);

cout<<"Addition is "<<s1.addition();

mul m1;

m1.getop(3,4);

cout<<"\n Multiplication is "<<m1.multiplication();

return 0;

}

Page 15: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Multilevel Inheritance:

• Multilevel Inheritance: a Derived class is a base class for anotherclass.

class A

{

//content of base class i.e., A

};

class B :public A

{

//content of class i.e., B

};

class C :public B

{

//content of derived class i.e., C

};

Page 16: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Example for Multilevel Inheritance:#include<iostream>

using namespace std;

class person{

protected:

int age;

char name[10];

public:

void getdata(int a, char b[]){

age=a;

for(int i=0;i<10;i++)

name[i]=b[i];

}

};

class student: public person{

protected:

char reg[10];

public:

void getstu(){

cout<<"\n Enter the Register number :";

cin>>reg; }

void show(){

cout<<"\n student information \n ";

cout<<"name :"<<name<<"\n";

cout<<"age :"<<age<<"\n";

cout<<"RollNO :"<<reg<<"\n";

}

};

class nitr: public student{

protected:

char branch[10];

int y;

public:

void getcol(){

cout<<"\n Enter the Branch :";

cin>>branch;

cout<<"\n Enter the Year :";

cin>>y;

}

void totalDATA(){

cout<<"\n student information \n ";

cout<<"name :"<<name<<"\n";

cout<<"age :"<<age<<"\n";

cout<<"RollNO :"<<reg<<"\n";

cout<<"Branch :"<<branch<<"\n";

cout<<"year :"<<y<<"\n";

}

};

Page 17: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

int main(){

nitr p1;

int a;

char b[10];

cout<<"Enter the name ";

cin>>b;

cout<<"Enter the age";

cin>>a;

p1.getdata(a,b);

p1.getstu();

p1.getcol();

p1.totalDATA();

return 0;

}

Page 18: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Hybrid Inheritance:Hybrid Inheritance (also known as Virtual Inheritance): Hybrid Inheritance is combination of Hierarchical and Mutilevel Inheritance.

class A

{

//content of base class i.e., A

};

class B :public A

{

//content of class i.e., B

};

class C :public B

{

//content of derived class i.e., C

};

Class D: public B, public C{

}

Page 19: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Try the following Example for hybrid Inheritanceclass Grandparent

{ //content of grandparent class

};

class Child1 :public virtual Grandparent

{ //content of Child1 class

};

class Child2 :public virtual Grandparent

{ //content of Child2 class

};

class grandson :public Child1, public Child2

{ //content of grandson class

};

Page 20: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Assignment1 (Date: - 9/01/2017)

1. Write a sample C++ program demonstrating Variable scope.

2. Write a C++ program for finding factorial of a given number using recursion.

3. Write a C++ program to find the roots of a quadratic equation. Use “switch” or “if” statements to handle different values of the discriminant (b2 – 4ac).

4. Write a C++ program to create student class, which contains name, Register-Number & 5-subject marks as data members calculate average & grade by using member functions.

Page 21: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Assignment2 (Date: - 16/01/2017)

1. Write a C++ program to calculate area of rectangle and square using constructor (default) and destructor

2. Write a C++ program to calculate area of circle using constructor (parameterized) and destructor

3. Write a C++ program to calculate area of triangle using constructor (copy) and destructor

Page 22: Inheritance - bphanikrishna.files.wordpress.com 23, 2016 · Write a C++ program for finding factorial of a given number using ... C++ program to find the roots of ... area of triangle

Assignment3 (Date: - 23/01/2017)

Write C++ program to ready student information such as Name, DOB,Address (village/city, district, state and country, pin code), Educationdetails like RegNO, Department, Course (b-tech, m-tech or dual degree)by illustrating following Inheritance concepts.

1. Single Inheritance

2. Multiple Inheritance

3. Hierarchical Inheritance

4. Multilevel Inheritance

5. Hybrid Inheritance (also known as Virtual Inheritance)