22F7470 Lab11
22F7470 Lab11
EQUIPMENT:
Computer equipped with Visual Studio / Dev C++ and Microsoft Office.
BACKGROUND:
Polymorphism is a core concept in object-oriented programming (OOP) that allows objects of different
types to be treated as instances of the same type through a shared interface. The term comes from the
Greek words poly (meaning "many") and morph (meaning "forms"), reflecting the ability of a single
function or operator to operate in multiple ways.
In C++, polymorphism is often achieved through inheritance and function overriding, enabling derived
classes to provide specific implementations for methods defined in a base class.
Example:
#include <iostream>
using namespace std;
int main() {
Animal genericAnimal;
Dog dog;
Cat cat;
return 0;
}
An abstract class is a class that cannot be instantiated on its own and usually contains at least one pure
virtual function. It serves as a blueprint for other classes. Concrete classes are classes that implement all
methods and can be instantiated. This means you cannot create an object of an abstract class because it
typically contains one or more pure virtual functions (functions declared with = 0), which have no
CS1004 – Object Oriented Programming Lab manual
implementation in the abstract class. Abstract classes act as blueprints for derived classes, ensuring that
certain functions will be defined in any concrete subclass.
A pure virtual function is a function with = 0 in its declaration. Classes with pure virtual
functions are abstract and cannot be instantiated. They are typically used to define an interface (a
set of functions that derived classes must implement). Interfaces allow different classes to share a
common structure without sharing implementation details.
LAB TASKS:
Problem 1:
Design an object-oriented C++ program that calculates and displays the area of different geometric shapes
using polymorphism and abstract classes. The program should meet the following specifications:
• Abstract Base Class Shape: Define an abstract base class Shape to represent a generic shape.
Include two pure virtual functions:
o Enter_data(): Prompts the user to enter specific data for a shape (e.g., dimensions).
o Area(): Calculates and displays the area of the shape.
• Derived Classes:
• Create a class Rectangle that inherits from Shape:
o Private members: length and breadth. o Implement Enter_data() to prompt the user for length and
breadth.
o Implement Area() to calculate and display the area of the rectangle as length * breadth. Create a
class Circle that also inherits from Shape:
o Private member: radius.
o Implement Enter_data() to prompt the user for radius.
CS1004 – Object Oriented Programming Lab manual
o Implement Area() to calculate and display the area of the circle using the formula π * radius *
radius (use 3.14 as an approximation for π).
• Main Program:
o Use a pointer of type Shape* to access objects of the derived classes and call their respective
methods through this pointer. o First, point the Shape pointer to an instance of Rectangle to prompt
the user for dimensions and display the rectangle's area. o Then, point the Shape pointer to an
instance of Circle to prompt the user for the radius and display the circle's area.
Sample Output:
Code:
#include <iostream>
using namespace std;
class Shape {
public:
virtual void Enter_data() = 0;
virtual void Area() = 0;
};
public:
void Enter_data() override {
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter breadth of the rectangle: ";
cin >> breadth;
}
};
public:
void Enter_data() override {
cout << "Enter radius of the circle: ";
cin >> radius;
}
int main() {
Shape* shapePtr;
Rectangle rect;
shapePtr = ▭
shapePtr->Enter_data();
shapePtr->Area();
Circle circ;
shapePtr = ˆ
shapePtr->Enter_data();
shapePtr->Area();
return 0;
}
Output:
CS1004 – Object Oriented Programming Lab manual
Problem 2:
Create a C++ program that represents a greeting card system for different occasions using
abstract classes and polymorphism. The program should satisfy the following requirements:
• Abstract Base Class Card: o Define an abstract base class named Card that represents a
generic card.
o Include a protected member recipient to store the recipient’s name. o Declare a pure
virtual function greeting() to define the customized message for each occasion.
• Derived Classes :Implement the following derived classes from Card, each providing a
unique greeting based on the occasion:
o Holiday Class:
Constructor: Accepts the recipient’s name and stores it in the recipient attribute.
greeting() function: Displays a holiday greeting message for the recipient.
o Birthday Class: Private member: age to store the age of the recipient.
Constructor: Accepts the recipient’s name and age, storing them in recipient and age
attributes, respectively. greeting() function: Displays a personalized birthday greeting for
the recipient that includes their age. o Holi Class:
Private member: colors to represent the number of colors used for the Holi greeting.
Constructor: Accepts the recipient’s name and a number representing colors, storing them in
recipient and colors. greeting() function: Displays a Holi greeting for the recipient with
a colorful representation using stars (*) based on the value of colors.
• Main Program:
Create objects of each derived class (Holiday, Birthday, and Holi) and invoke the greeting()
function for each to display a customized message for each occasion.
Code:
#include <iostream>
#include <string>
using namespace std;
class Card {
CS1004 – Object Oriented Programming Lab manual
protected:
string recipient;
public:
Card(const string& name) : recipient(name) {}
virtual void greeting() = 0;
};
public:
Birthday(const string& name, int ageVal) : Card(name), age(ageVal) {}
public:
Holi(const string& name, int colorsVal) : Card(name), colors(colorsVal) {}
cout << "Happy Holi and lots of colors for you" << endl;
for (int i = 0; i < colors; ++i) {
cout << "*";
}
cout << endl;
}
};
int main() {
Card* card;
Holiday holidayCard("Codez");
card = &holidayCard;
card->greeting();
return 0;
}
Output:
CS1004 – Object Oriented Programming Lab manual
Instructor’s Signature:______________