Unit - 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

OBJECT ORIENTED PROGRAMMING METHODOLOGY

UNIT – III
SL No. Index
1 Relationships- Inheritance: purpose and its types
2 IS-A Relationships
3 Association
4 Aggregation
5 Interfaces
6 Abstract classes
Inheritance
The capability of a class to derive properties and characteristics from another class is called
Inheritance. Inheritance is one of the most important features of Object-Oriented
Programming.
Inheritance is a feature or a process in which, new classes are created from the existing
classes. The new class created is called “derived class” or “child class” and the existing class
is known as the “base class” or “parent class”. The derived class now is said to be inherited
from the base class.
When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class.
Derived Class: The class that inherits properties from another class is called Subclass or
Derived Class.
Base Class: The class whose properties are inherited by a subclass is called Base Class or
Superclass.
Advantage of C++ Inheritance
Code reusability: Reuse the members such as variables, methods defined in your parent
class. There is no need to define the member again so less code is required in the class.
Types of Inheritance
Five types of inheritance:
 Single inheritance
 Multiple inheritance
 Hierarchical inheritance
 Multilevel inheritance
 Hybrid inheritance
When the base class is privately inherited by the derived class, public members of the base
class becomes the private members of the derived class. The public members of the base class
are not accessible by the objects of the derived class only by the member functions of the
derived class.
The base class is publicly inherited by the derived class; public members of the base class
also become the public members of the derived class. Therefore, the public members of the
base class are accessible by the objects of the derived class as well as by the member
functions of the base class.
Note:
 In C++, the default mode of visibility is private.
 The private members of the base class are never inherited.
C++ Single Inheritance
Single inheritance is defined as the inheritance in which a derived class is inherited from the
only one base class.

Where 'A' is the base class and 'B' is the derived class.
C++ Single Level Inheritance Example: Inheriting Fields
When one class inherits another class, it is known as single level inheritance. The example of
single level inheritance which inherits the base class variables only.
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main()
{
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output:
Salary: 60000
Bonus: 5000
In the above example, Employee is the base class and Programmer is the derived class.
C++ Single Level Inheritance Example: Inheriting Methods
Another example of inheritance in C++ which inherits methods only.
#include <iostream>
using namespace std;
class Vehicle {
public:
void engine() {
cout<<"Engine started"<<endl;
}
};
class Car: public Vehicle
{
public:
void car_break(){
cout<<"Breaking";
}
};
int main() {
Vehicle d1;
d1.engine();
d1.car_break);
return 0;
}
Output:
Engine Started...
Breaking...
Let's see a simple example.
#include <iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};
class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();
return 0;
}
Output:
Multiplication of a and b is: 20
In the above example, class A is privately inherited. Therefore, the mul() function of class 'A'
cannot be accessed by the object of class B. It can only be accessed by the member function
of class B.
How to make a Private Member Inheritable
The private member is not inheritable. If we modify the visibility mode by making it public,
but this takes away the advantage of data hiding. C++ introduces a third visibility modifier,
i.e., protected. The member who is declared as protected will be accessible to all the member
functions within the class as well as the class immediately derived from it.
Visibility modes can be classified into three categories:

 Public: When the member is declared as public, it is accessible to all the functions of the
program.
 Private: When the member is declared as private, it is accessible within the class only.
 Protected: When the member is declared as protected, it is accessible within its own class
as well as the class immediately derived from it.
Visibility of Inherited Members
Base class visibility Derived class visibility
Public Private Protected
Private Not Inherited Not Inherited Not Inherited
Protected Protected Private Protected
Public Public Private Protected
Multilevel Inheritance
Multilevel inheritance is a process of deriving a class from another derived class.

Multi Level Inheritance Example


When one class inherits another class which is further inherited by another class, it is known
as multi level inheritance in C++. Inheritance is transitive so the last derived class acquires all
the members of all its base classes. Example of multi level inheritance.
#include <iostream>
using namespace std;
class Vechicle{
public:
void start() {
cout << "Start..." << endl;
}
};
class Car : public Vechicle
{
public:
void stop() {
cout << "Stop" << endl;
}
};
class Bike : public Car
{
public:
void run() {
cout << "Bike is Running...";
}
};
int main(void) {
Bike d1;
d1.start();
d1.stop();
d1.run();
return 0;
}
Output:
Start
Stop
Bike is running
C++ Multiple Inheritance
Multiple inheritances are the process of deriving a new class that inherits the attributes from
two or more classes.

Simple example of multiple inheritance.


#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a(int n)
{
a = n;
}
};
class B
{
protected:
int b;
public:
void get_b(int n)
{
b = n;
}
};
class C : public A,public B
{
public:
void display()
{
cout << "The value of a is : " <<a<< std::endl;
cout << "The value of b is : " <<b<< std::endl;
cout<<"Addition of a and b is : "<<a+b;
}
};
int main()
{
C c;
c.get_a(10);
c.get_b(20);
c.display();
return 0;
}
Output:
The value of a is : 10
The value of b is : 20
Addition of a and b is: 30
In the above example, class 'C' inherits two base classes 'A' and 'B' in a public mode.
Ambiguity Resolution in Inheritance
Ambiguity can be occurred in using the multiple inheritances when a function with the same
name occurs in more than one base class. Let's understand this through an example:
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
std::cout << "Class A" << std::endl;
}
};
class B
{
public:
void display()
{
std::cout << "Class B" << std::endl;
}
};
class C : public A, public B
{
void view()
{
display();
}
};
int main()
{
C c;
c.display();
return 0;
}
Output:
error: reference to 'display' is ambiguous
display();
The above issue can be resolved by using the class resolution operator with the function. In
the above example, the derived class code can be rewritten as:
class C : public A, public B
{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.
}
};
An ambiguity can also occur in single inheritance.
Consider the following situation:
class A
{
public:
void display()
{
cout<<?Class A?;
}
};
class B
{
public:
void display()
{
cout<<?Class B?;
}
};
In the above case, the function of the derived class overrides the method of the base class.
Therefore, call to the display () function will simply call the function defined in the derived
class. If we want to invoke the base class function, we can use the class resolution operator.
int main()
{
B b;
b.display(); // Calling the display() function of B class.
b.B :: display(); // Calling the display() function defined in B class.
}
C++ Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of inheritance.

Let's see a simple example:


#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
cout << "Enter the value of 'a' : " << endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
cout << "Enter the value of 'b' : " << endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Output:
Enter the value of ‘a’:
10
Enter the value of ‘b’:
20
Enter the value of c is:
30
Multiplication of a,b,c is : 6000
C++ Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than one class from a base
class.

Let's see a simple example:


#include <iostream>
using namespace std;
class Shape // Declaration of base class.
{
public:
int a;
int b;
void get_data(int n,int m)
{
a= n;
b = m;
}
};
class Rectangle : public Shape // inheriting Shape class
{
public:
int rect_area()
{
int result = a*b;
return result;
}
};
class Triangle : public Shape // inheriting Shape class
{
public:
int triangle_area()
{
float result = 0.5*a*b;
return result;
}
};
int main()
{
Rectangle r;
Triangle t;
int length,breadth,base,height;
cout << "Enter the length and breadth of a rectangle: " << std::endl;
cin>>length>>breadth;
r.get_data(length,breadth);
int m = r.rect_area();
cout << "Area of the rectangle is : " <<m<< std::endl;
cout << "Enter the base and height of the triangle: " << std::endl;
cin>>base>>height;
t.get_data(base,height);
float n = t.triangle_area();
std::cout <<"Area of the triangle is : " << n<<std::endl;
return 0;
}
Output:
Enter the length and breadth of a rectangle:
23
20
Area of the rectangle is : 460
Enter the base and height of the triangle:
2
5
Area of the triangle is: 5
IS-A Relationship
In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which
can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B
type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-
directional. For example, House is a Building. But Building is not a House. You can easily
identify the IS-A relationship. Wherever you see an : keyword in a class declaration, then this
class is said to have IS-A relationship. The is-a relationship, also known as the inheritance
relationship, represents a type of relationship between two classes where one class is a
specialized version of another. It implies that a subclass is a specific type of its superclass.
Benefits of Is-A Relationship:
The is-a relationship provides several advantages in Java programming:
 Code Reusability: Inheritance allows subclasses to inherit the properties and behaviors
of the superclass. This promotes code reuse, as the subclass can leverage the existing code
of the superclass without having to rewrite it.
 Polymorphism: Polymorphism, a key feature of object-oriented programming, is closely
related to the is-a relationship. It allows objects of different classes to be treated as objects
of a common superclass. This enables more flexibility and extensibility in the design and
implementation of software.
 Method Overriding: Subclasses can override methods inherited from the superclass to
provide their own implementation. This allows for customization and specialization of
behavior according to the specific needs of the subclass while still maintaining the is-a
relationship.
To establish an is-a relationship between classes in Java, the keyword "extends" is used. The
subclass extends the superclass, indicating that it inherits all the members (fields and
methods) of the superclass. The is-a relationship in C++ is a fundamental aspect of object-
oriented programming that allows for the creation of class hierarchies. It enables code
reusability, polymorphism, and method overriding, promoting better organization and
extensibility of software. By understanding and leveraging the is-a relationship, developers
can design more robust and flexible C++ applications.
Association
Association: An association is defined as an organization of people with a common purpose
and having a formal structure. It represents a binary relationship between two objects that
describes an activity. It is a relationship between objects. For example, A doctor can be
associated with multiple patients.

 Association is a relationship between two classes where one class uses another.
 It is inflexible in nature
 It means there is almost always a link between objects
 It is represented by a “has a” relationship.
 Line segment is used between the components or the class.
In one-to-one relationships, an entity of one database is uniquely related to an entity of
another database. For example, if we consider customer table and address table related to
customer table, then customer table and address table are said to be in one-to-one relationship
with each other.
One-to-many and many-to-one relationships – In this kind of relationships, one entity of a
database may be related to one or more entities of another database and vice versa. For
example, if we consider the same customer table as in Table 2.1 and assume the online
transaction on any e-commerce website, then we may consider the facts that
 One customer can make many orders.
 More than one item can be made in one order.
Now considering these two situations, we will have one-to-many relationships.

Many-to-many relationships – In this kind of relationships, multiple entities of one database


may be related to more than one entity of another database and an entity in the second
database may be related to many entities in the other database. That means multiple instances
exist between the relationships. For example, in every order we can have multiple items and
each item may be there in multiple orders. All we need to do here is to create one more table
which can relate to the order and item table. The only purpose of this table is to create many-
to-many relationships.
Considering the order table as in Table 2.3 and taking another table called items table as
shown in Table 2.4, we need to create an additional table called items_order table, as shown
in Table 2.5.

Aggregation: An aggregation is a collection or the gathering of things together. This


relationship is represented by a “has a” relationship. In other words, aggregation is a group,
body, or mass composed of many distinct parts or individuals For example, phone number list
is an example of aggregation.

 Aggregation describes a special type of an association which specifies a whole and part
relationship.
 It in flexible in nature
 Special kind of association where there is whole-part relation between two objects.
 It is represented by a “has a”+ “whole-part” relationship.
 Diamond shape structure is used next to the assembly class.
Association is a relation between two separate classes which establishes through their
Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many. In
Object-Oriented programming, an Object communicates to another object to use functionality
and services provided by that object. Composition and Aggregation are the two forms of
association.
It is a special form of Association where:
 It represents Has-A’s relationship.
 It is a unidirectional association i.e. a one-way relationship. For example, a department
can have students but vice versa is not possible and thus unidirectional in nature.
 In Aggregation, both entries can survive individually which means ending one entity
will not affect the other entity.

Aggregation
It represents a Has-A relationship. In the above example: Student Has-A name. Student Has-
A ID. Student Has-A Dept. Department Has-A Students as depicted from the below media.

Composition
Composition
Composition is a restricted form of Aggregation in which two entities are highly dependent
on each other.
 It represents part-of relationship.
 In composition, both entities are dependent on each other.
 When there is a composition between two entities, the composed object cannot exist
without the other entity.
Aggregation vs. Composition
Dependency: Aggregation implies a relationship where the child can exist independently of
the parent. For example, Bank and Employee, delete the Bank and the Employee still exist.
whereas Composition implies a relationship where the child cannot exist independent of the
parent. Example: Human and heart, heart don’t exist separate to a Human
Type of Relationship: Aggregation relation is “has-a” and composition is “part-of”
relation.
Type of association: Composition is a strong Association whereas Aggregation is a weak
Association.
Interface
Here in C++, No concept of Interface. We study interface through java. Interface in Java is
a blueprint of a class. It has static constants and abstract methods. The interface in Java is a
mechanism to achieve abstraction. There can be only abstract methods in the Java interface,
not method body. It is used to achieve abstraction and multiple inheritances in Java. You can
say that interfaces can have abstract methods and variables. It cannot have a method body.
Java Interface also represents the IS-A relationship. It cannot be instantiated just like the
abstract class. Since Java 8, we can have default and static methods in an interface. Since
Java 9, we can have private methods in an interface. There are mainly three reasons to use
interface. They are given below.
 It is used to achieve abstraction.
 By interface, we can support the functionality of multiple inheritances.
 It can be used to achieve loose coupling.
 Normal or Simple method is called concrete method.
 In interface we declare only Abstract method in interface.
 It is used to achieve abstraction.
 it supports multiple Inheritance.
 Interface support loose coupling.
Example :
interface Interface_Name
{
Methods
Fields
}
 Only abstract method not concrete method declare in Interface.
 All method are public declared externally. If not declare public, Compiler automatically
declare public internally.
 By default, method and fields are public, static, final declaration.
Note : In JDK version 1.8, JDK 1.9 and Modern Java JDK, we declare concrete method in
Interface, but access specifier of concrete method declared in Interface must be default
Access. But In case Abstract method, access specifier is must be public, static and final
externally declared by programmer. And internally declared by java compiler. In JDK 1.9 we
also create private concrete method in Interface.
Example:
interface I1
{
void show();
}
class Test implements I1
{
public void show();
{
System.out.println(“1”);
}
public static void main(String[] args)
{
Test t = new Test();
t.show();
}
}
In Interface we create method, it is not mandatory to insert abstract keyword into method.
All variables is in Interface is public static final type.
Interface I1
{
void show();
}
interface I2
{
void display();
}
class Test implements I1, I2
{
public void show()
{
System.out.println(“1”);
}
public void display()
{
System.out.println(“2”);
}
public static void main(String[] args)
{
Test t = new Test();
t1.show();
t1.display();
}
}
Interface Implementation
Think of an interface as a 100% abstract class. Like an abstract class an interface defines
abstract method that take the form.
abstract void bounce()
An abstract class can define both abstract and non abstract methods, but an interface can have
only abstract methods.
Interface Rules
 All interface methods are implicitly public and abstract.
 Interface methods must not be static.
 We can create class in interface using jdk 1.8 or later.
 We can create concrete method in interface but declared concrete method must be static.
Otherwise give error without static.
 We can not use static keyword with abstract method.
 Because interface methods are abstract, they cannot be marked as final, native, strictfp, or
synchronized.
 You do not need to actually type the public or an abstract modifiers in the method
declaration, The method still always public and abstract.
 Variable deceleration in interface must be initialized.
 All variable defined in interface must be public, static and final, in other words the
interface declares only constants not instance variable.
 An interface can extend one or more interfaces.
 An interface extends another interface.
 A Class implements Interface.
 An interface cannot implements another interface or class.
 An interface must be declared with the keyword interface.
 Interface type can be used polymorphically.
 The following is a legal interface deceleration:
public abstract interface Rollable()
 Interface are implicitly abstract whether you type or not.
public abstract interface Rollable()
public interface Rollable()
 The public modifier is required if you want the interface to have public rather than
default. If declared public its must me declared its own separate file.
Interface methods declared with any combination of public, abstract or no modifiers. For
example, the following five method declarations. If declared within on interface are legal and
identical
void bounce(); // OK
public void bounce(); //OK
abstract void bounce(); //OK
public abstract void bounce(); //OK
abstract public void bounce(); //OK
The following interface method declaration won’t compile:
final void bounce(); //final and abstract can never be used
static void bounce(); //interfaces define instance methods
private void bounce(); //interface methods are always public
protected void bounce(); // (same as above)
synchronized void bounce(); //can’t mix synchronized and abstract
native void bounce(); // can’t mix abstract and native
strictfp void bounce(); // can not mix abstract and strictfp
Declaring Interface Constants
Any class implementing the interface will have access to the same constants.
public final static int LOW_BOUNCE = 4;
public final static int LOW_BOUNCE = 3;
if a method takes the int values,
public void animateIt(int gravity, int bounceFactor)
{
}
Then the code that calls animateIt() can substitute the constants, whenever the int values are
expected as follows:
animator.animateIt(LOW_GRAVITY, HIGH_BOUNCE);
Any class that implements the interface has direct access to the constants, just as if the class
had inherited them. A few rules for interface constants :
public
static
final
 Interface constants are no different from any other publicly accessible constants so they
obviously must be declared public, static and final.
 Any variable defined in an interface must be and implicitly is a public constant.
 You can’t change the value of constant once the value is assigned to the constants. This
assignment happens in the interface itself (where the constant is declared). So the
implementing class can access it an use it but as a read-only value.
Look interface definition that defines constants , but without explicitly using the required
modifiers. For example the following are all identical:
public int x = 1; //looks non static and non final, but isn’t
int x = 1; // looks default non final, and non-static but isn’t.
static int x = 1; //doesn't show final or public
final int x = 1 //doesn’t show static or publicly
public static int x = 1; // doesn’t show final
public final int x = 1; //Doesn’t show static
static final int x = 1; //doesn’t show public
public static final int x = 1; // exactly what you get implicitly.
Any combination of the required (but implicit) modifiers is legal as is using no modifiers at
all.
Implementing an Interface
Implementing classes must adhere to the same rule for method implementing as a class
extending an abstract class. In order to be legal implementation class, a non abstract
implementation class must do the following:
 Provide concrete (non abstract) implementations for all method form the declared
interface.
 Follow all the rules for legal overrides
 Declare no checked exceptions on implementations methods other than those declared by
the interface method, or subclass of those declared by the interface method.
 Maintaining the signature of the interface method, and maintain the same return type( but
does not have to declare the exceptions declared in the interface method declaration).
A class can extends more than one interface using extend keyword. And Interface is also
extend to another interfaces using the extend keyword. In the case of class use implement
keyword .
public class Ball implements Bounceable, Serializable, Runnable{…}
Interface C extends A, B // Legal
You can extend only one class, but implement many. But remember that sub-classing defines
who and what you are, where implementing defines a role you can play or a hat you can
wear, despite how different you might be from some other class implementing the same
interface. An interface cal itself extend another interface, but never implement anything.
The following code is perfectly legal:
public interface Bounceable extends Movable { }
 The first concrete(no abstract) implementations class of Bounceable must implement all
the methods of Bounceable, plus all the methods of Movable.
 An interface can extends more than one interface.
 A class is not allowed to extend multiple classes in java. An interface however is free to
extend multiple interfaces. This is legal to declare interface into public access class.

public class Test


{
interface D extends A //Legal
{
}
}
Note:
 class zoo() //ok
 class Bar implements Zoo{} // no, can’t implement a class
 interface Bar //ok
 interface Fi //ok
 interface Fee implements Bar{ }
//No, interface can’t implement on interfaces
 interface Zee implement Zoo{ }
// No, interface can’t implement class
 interface zee extends Zoo{ }
// no interface can’t extend a class
 interface Boo extend Fi { }
// ok, interface extend interfaces
 class Toon extends Foo, Button { }
// No, class can’t extend multiple class
 class zoom implements Fi, Fee{}
// Ok, class can implement multiple interface
 interface Vroom extends Fi, Fee { }
// ok interface can extend multiple interface.
Abstract class in C++
By definition, a C++ abstract class must include at least one pure virtual function.
Alternatively, put a function without a definition. Because the subclass would otherwise turn
into an abstract class in and of itself, the abstract class's descendants must specify the pure
virtual function. Broad notions are expressed using abstract classes, which can then be
utilized to construct more specific classes. You cannot make an object of the abstract class
type. However, pointers and references can be used to abstract class types. When developing
an abstract class, define at least one pure virtual feature. A virtual function is declared using
the pure specifier (= 0) syntax. Consider the example of the virtual function. Although the
class's objective is to provide basic functionality for shapes, elements of type shapes are far
too general to be of much value. Because of this, the shape is a good candidate for an abstract
class:
Code
Class classname //abstract class
{
//data members
public:
//pure virtual function
/* Other members */
};
Characteristics of abstract class
Although the Abstract class type cannot be created from scratch, it can have pointers and
references made to it. A pure virtual function can exist in an abstract class in addition to
regular functions and variables. Up-casting, which lets derived classes access their interface,
is the main usage of abstract classes. Classes that descended from an abstract class must
implement all pure virtues.
Restrictions to abstract class
The following uses of abstract classes are not permitted:
1. Conversions made consciously
2. Member data or variables
3. Types of function output
4. Forms of debate
It is unknown what happens when a pure virtual method is called explicitly or indirectly by
the native code function Object () of an abstract class. Conversely, abstract group
constructors and destructors can call additional member functions. Although the constructors
of the abstract class are allowed to call other member functions, if they either directly or
indirectly call a pure virtual function, the outcome is unknown. A member function that has
been redefined by a derived class from a base class declaration is referred to as a virtual
function. A virtual function that lacks definition or logic is known as an abstract function or a
pure virtual function. At the time of declaration, 0 is assigned to it.
Important Abstraction is in Daily Life
The ATM machine is another example of abstraction in everyday life;. However, we all use
the ATM to do tasks like cash withdrawal; money transfers, generating mini-statements, and
so on, and we have no access to the ATM's internal data. Data protection techniques like data
abstraction can prevent unauthorized access to data.
Difference between abstract class and interface
Interface Abstract class
An interface can only inherit from another With the Extended keyword, an abstract
interface. class can enforce an interface and inherit
from another class.
Use of the implements keyword is required Use the extends keyword to inherit from an
in order to implement an interface. abstract class.
Example of an abstract class
Consider developing a calculator that will output the shape's perimeter when it is entered.
Consider the type of programming you would use to create such a calculator. By creating
distinct functions inside the Shape class, you may start with a few basic forms and hardcode
the perimeter. This is how the class might appear:
Example of an abstract class
Consider developing a calculator that will output the shape's perimeter when it is entered.
Consider the type of programming you would use to create such a calculator. By creating
distinct functions inside the Shape class, you may start with a few basic forms and hardcode
the perimeter. This is how the class might appear:
class Shape {
public:
// All the functions of both square and rectangle are clubbed together in a single class.
void width(int w) {
shape_width = w;
}
void height(int h) {
shape_height = h;
}
int areaOfSquare(int s) {
return 4 * s;
}
int areaOfRectange(int l, int b) {
return (l * b);
}
protected:
int shape_width;
int shape_height;
};
int main (){
shapes R;
R.width(5);
R.height(10);
cout<<"The area of rectangle is"<<R.areaOfRectangle";
return 0;
}
Output:
The area of the rectangle is: 50
While OOP advises that we should attempt to adhere to real-world reasoning, this will still
function. As a result, we can create the class Shape as the parent class, with the Classes
Square and Rectangle acting as the children. If you want to add something new later, you can
do it in the child class, which will make the program simpler to maintain. We must use
Abstract Classes to put this feature into practice. At least one pure virtual function is required
for abstract classes in C++. In order to prevent the subclass from becoming an abstract class,
the abstract class's inheriting classes must specify the pure virtual function.

You might also like