0% found this document useful (0 votes)
15 views

22F7470 Lab11

22F7470_Lab11

Uploaded by

omaishmalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

22F7470 Lab11

22F7470_Lab11

Uploaded by

omaishmalik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS1004 – Object Oriented Programming Lab manual

EXPERIMENT 11 Date Perform:

To learn about Polymorphism , Abstract and concrete classes,


Abstract Classes & pure Virtual Functions (Interface vs.
Implementation)
OBJECTIVE:
Enable students to learn and implement the following concepts in c++
 Polymorphism
 Abstract and concrete classes
 Pure Virtual Functions

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.

class Base { public:


virtual void show() { /* Base implementation */ }
};
CS1004 – Object Oriented Programming Lab manual

class Derived : public Base {


public:
void show() override { /* Derived implementation */ } };

Example:
#include <iostream>
using namespace std;

class Animal { public:


virtual void sound() {
cout << "Animal sound\n";
} };

class Dog : public Animal {


public:
void sound() override {
cout << "Woof!\n";
} };

class Cat : public Animal { public:


void sound() override {
cout << "Meow!\n";
} };

void makeSound(Animal* animal) { animal-


>sound();
}

int main() {
Animal genericAnimal;
Dog dog;
Cat cat;

makeSound(&genericAnimal); // Calls Animal's sound


makeSound(&dog); // Calls Dog's sound
makeSound(&cat); // Calls Cat's sound

return 0;
}

Abstract and Concrete Classes

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.

class AbstractClass { public:


virtual void abstractMethod() = 0; // Pure virtual function
};

class ConcreteClass : public AbstractClass { public:


void abstractMethod() override { /* Concrete implementation */ }
};

Pure Virtual Functions (Interface vs. Implementation)

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.

• Interface: Provides only the declarations of methods (pure virtual functions).


Implementation: The concrete class provides the actual functionality.

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

class Rectangle : public Shape {


private:
double length;
double breadth;

public:
void Enter_data() override {
cout << "Enter length of the rectangle: ";
cin >> length;
cout << "Enter breadth of the rectangle: ";
cin >> breadth;
}

void Area() override {


double area = length * breadth;
cout << "Area of the rectangle: " << area << endl;
}
CS1004 – Object Oriented Programming Lab manual

};

class Circle : public Shape {


private:
double radius;

public:
void Enter_data() override {
cout << "Enter radius of the circle: ";
cin >> radius;
}

void Area() override {


double area = 3.14 * radius * radius;
cout << "Area of the circle: " << area << endl;
}
};

int main() {
Shape* shapePtr;

Rectangle rect;
shapePtr = &rect;
shapePtr->Enter_data();
shapePtr->Area();

Circle circ;
shapePtr = &circ;
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;
};

class Holiday : public Card {


public:
Holiday(const string& name) : Card(name) {}

void greeting() override {


cout << "Dear " << recipient << endl;
cout << "Season's Greetings!" << endl << endl;
}
};

class Birthday : public Card {


private:
int age;

public:
Birthday(const string& name, int ageVal) : Card(name), age(ageVal) {}

void greeting() override {


cout << "Dear " << recipient << "," << endl;
cout << "Happy " << age << "th Birthday" << endl << endl;
}
};

class Holi : public Card {


private:
int colors;

public:
Holi(const string& name, int colorsVal) : Card(name), colors(colorsVal) {}

void greeting() override {


cout << "Dear " << recipient << "," << endl;
CS1004 – Object Oriented Programming Lab manual

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();

Birthday birthdayCard("CodezClub", 21);


card = &birthdayCard;
card->greeting();

Holi holiCard("Max", 7);


card = &holiCard;
card->greeting();

return 0;
}
Output:
CS1004 – Object Oriented Programming Lab manual

CS1004 – Object Oriented Programming Lab


The focus of this lab is to cover object-oriented programming paradigm. It is aimed at helping the
students to analyze the problem, identify the objects involved and implement it using objectoriented
programming methods. C++ will be used as programming language for this lab.

CLO Statement ↓ Exemplary Proficient Developing Beginning Novice


Score → (5) (4) (3) (2) (1)
Demonstrate the Understand
concept of C- Attempts all Attempts Attempts lab the given
strings, recursion, lab tasks with partial lab tasks with concept but
bitwise operator correct output tasks with incorrect output unable to No attempt
1 and files correct output apply it
handling
(binary and text
file)..

Behave Completes Completes the


responsibly, the lab task lab task
3 and individually Completes with minor with help of Poorly
performs the help of instructor and Does not
the lab task on attempts lab
lab. instructor students work
his own tasks
around

CLO MARKS OBTAINED


1
3

Instructor’s Signature:______________

You might also like