Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Module 4 Part 1 Inheritance

This module covers object-oriented programming concepts in C++, focusing on inheritance and polymorphism. It explains the relationships between base and derived classes, access control, and multiple inheritance, along with practical examples. Additionally, it introduces function overloading, allowing multiple definitions for the same function name based on argument types or counts.

Uploaded by

annaleighvaldez2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module 4 Part 1 Inheritance

This module covers object-oriented programming concepts in C++, focusing on inheritance and polymorphism. It explains the relationships between base and derived classes, access control, and multiple inheritance, along with practical examples. Additionally, it introduces function overloading, allowing multiple definitions for the same function name based on argument types or counts.

Uploaded by

annaleighvaldez2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MANUEL V GALLEGO FOUNDATION COLLEGES, INC.

INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY

Computer Programming 1 ICT113

MODULE IV:

INTENDED LEARNING OUTCOMES:

Learners at the end of the module will be able to:

1. Explain the different concepts surrounding the object-oriented programming paradigm, and how they
help in creating efficient C++ programs
2. Create C++ programs with Inheritance and Polymorphism concepts.

COURSE CONTENT:

C++ Inheritance

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, which makes it easier to create and maintain an
application. This also 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.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A
mammal hence dog IS-A animal as well and so on.

Base and Derived Classes

A class can be derived from more than one classes, which means it can inherit data and functions from
multiple base classes. To define a derived class, we use a class derivation list to specify the base
class(es).

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.

Consider a base class Shape and its derived class Rectangle as follows –

#include <iostream>

using namespace std;

// Base class
class Shape {
public:

int width;
int height;

void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}

};

// Derived class
class Rectangle: public Shape {
public:

Prepared by: Mr. John Carlo L. Azarcon Page 1 of 4


MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY

Computer Programming 1 ICT113

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;
}

When the above code is compiled and executed, it produces the following result –

Total area: 35

Access Control and Inheritance

A derived class can access all the non-private members of its base class. Thus base-class members
that should not be accessible to the member functions of derived classes should be declared private in
the base class.

We can summarize the different access types according to - who can access them in the following way

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no

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.

Classifications of Inheritance

When deriving a class from a base class, the base class may be inherited through public, protected or
private inheritance. The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance, but public inheritance is commonly used. While using
different type of inheritance, following rules are applied −

1. Public Inheritance − When deriving a class from a public base class, public members of the
base class become public members of the derived class and protected members of the base
class become protected members of the derived class. A base class's private members are
never accessible directly from a derived class, but can be accessed through calls to the public
and protected members of the base class.

Prepared by: Mr. John Carlo L. Azarcon Page 2 of 4


MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY

Computer Programming 1 ICT113

2. Protected Inheritance − When deriving from a protected base class, public and protected
members of the base class become protected members of the derived class.

3. Private Inheritance − When deriving from a private base class, public and protected
members of the base class become private members of the derived class.

Multiple Inheritance

A C++ class can inherit members from more than one class.

Where access is one of public, protected, or private and would be given for every base class and they
will be separated by comma.

#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;
};

// Base class PaintCost


class PaintCost {
public:
int getCost(int area) {
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost {
public:
int getArea() {
return (width * height);
}
};

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;

Prepared by: Mr. John Carlo L. Azarcon Page 3 of 4


MANUEL V GALLEGO FOUNDATION COLLEGES, INC.
INSTITUTE OF INFORMATION AND COMPUTER TECHNOLOGY

Computer Programming 1 ICT113

return 0;
}

When the above code is compiled and executed, it produces the following result –

Total area: 35
Total paint cost: $2450

Function Overloading with OOP

You can have multiple definitions for the same function name in the same scope. The definition of the
function must differ from each other by the types and/or the number of arguments in the argument list.
You cannot overload function declarations that differ only by return type.

Following is the example where same function print() is being used to print different data types −

#include <iostream>
using namespace std;

class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(string c) {
cout << "Printing string: " << c << endl;
}
};

int main(void) {
printData pd;

// Call print to print integer


pd.print(5);

// Call print to print float


pd.print(500.263);

// Call print to print string


pd.print("Hello C++");

return 0;
}

When the above code is compiled and executed, it produces the following result –

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

Prepared by: Mr. John Carlo L. Azarcon Page 4 of 4

You might also like