Oop Theory Assignment2
Oop Theory Assignment2
#include<iostream>
using namespace std;
class shape{
public:
virtual void calculateArea()=0;
virtual ~shape(){}
};
}
};
int main(){
shape *ptr1= new circle;
shape *ptr2= new rectangle;
shape *ptr3= new triangle;
int x;
cout<<"1.Cricle"<<endl
<<"2.Rectangle"<<endl
<<"3.Triangle"<<endl
<<"4.Exit"<<endl;
while(x!=4){
cout<<"Choice the shape which area you want calculate
"<<endl;
cin>>x;
if(x==1){
ptr1->calculateArea();
}
else if(x==2){
ptr2->calculateArea();
}
else if(x==3){
ptr3->calculateArea();
}
}
delete ptr1,ptr2,ptr3;
return 0;
}
Question 2:
#include <iostream>
#include <string>
class Book {
public:
string title;
string author;
int main() {
int numberOfBooks;
cout << "Enter the number of books to allocate: ";
cin >> numberOfBooks;
releaseBooks(library);
return 0;
}
Question no 3:
#include <iostream>
#include <vector>
using namespace std;
class GraphicalElement {
public:
virtual void draw() const = 0;
virtual ~GraphicalElement() {}
};
int main() {
Line line;
Circle circle;
Rectangle rectangle;
vector<GraphicalElement*> elements;
elements.push_back(&line);
elements.push_back(&circle);
elements.push_back(&rectangle);
drawElements(elements);
return 0;
}
Question 4:
#include <iostream>
#include <string>
class Product {
public:
Product(const string& id, double price) : id(id), price(price) {}
virtual double getPrice() const = 0;
virtual ~Product() {}
protected:
string id;
double price;
};
int main() {
Product* products[2];
products[0] = new Electronics("E123", 299.99);
products[1] = new Clothing("C456", 49.99);
return 0;
}