0% found this document useful (0 votes)
2 views12 pages

Oop Theory Assignment2

The document contains multiple C++ programming assignments focusing on Object-Oriented Programming (OOP) concepts. It includes implementations of classes for shapes, books, graphical elements, and products, demonstrating polymorphism, dynamic memory allocation, and inheritance. Each assignment features a main function that interacts with user input to perform calculations or display information based on the defined classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views12 pages

Oop Theory Assignment2

The document contains multiple C++ programming assignments focusing on Object-Oriented Programming (OOP) concepts. It includes implementations of classes for shapes, books, graphical elements, and products, demonstrating polymorphism, dynamic memory allocation, and inheritance. Each assignment features a main function that interacts with user input to perform calculations or display information based on the defined classes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

OOP THEORY ASSIGNMENT # 2

NAME: Khizar Ahmad


ROLL NUMBER: 074
Question 1:

#include<iostream>
using namespace std;
class shape{
public:
virtual void calculateArea()=0;
virtual ~shape(){}
};

class circle: public shape{


float radius;
float r;
public:
void calculateArea() override{
cout<<"Enter the radius of the circle "<<endl;
cin>>radius;
r=3.14*radius*radius;
cout<<"Area of the circle is : "<<r<<endl;
}
};

class rectangle: public shape{


float length;
float width;
float r2;
public:
void calculateArea() override{
cout<<"Enter the length of rectangle "<<endl;
cin>>length;
cout<<"Enter the wdth of rectangle "<<endl;
cin>>width;
r2=length*width;
cout<<"Area of the rectangle is : "<<r2<<endl;
}
};

class triangle: public shape{


float base;
float height;
float r3;
public:
void calculateArea() override{
cout<<"Enter the base of the triangle "<<endl;
cin>>base;
cout<<"Enter the height of the triangle "<<endl;
cin>>height;
r3=0.5*base*height;
cout<<"Area of the triangle is "<<r3<<endl;

}
};

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>

using namespace std;

class Book {
public:
string title;
string author;

Book() : title(""), author("") {}

Book(string t, string a) : title(t), author(a) {}


};
Book* allocateBooks(int numberOfBooks) {
return new Book[numberOfBooks];
}

void releaseBooks(Book* books) {


delete[] books;
}

int main() {
int numberOfBooks;
cout << "Enter the number of books to allocate: ";
cin >> numberOfBooks;

Book* library = allocateBooks(numberOfBooks);

for (int i = 0; i < numberOfBooks; ++i) {


cin.ignore();
cout << "Enter title for book " << i + 1 << ": ";
getline(cin, library[i].title);
cout << "Enter author for book " << i + 1 << ": ";
getline(cin, library[i].author);
}
cout << "\nBooks in the library"<<endl;
for (int i = 0; i < numberOfBooks; ++i) {
cout << "Book " << i + 1 << ": " << library[i].title
<< " by " << library[i].author << "\n";
}

releaseBooks(library);

return 0;
}

Question no 3:
#include <iostream>
#include <vector>
using namespace std;

class GraphicalElement {
public:
virtual void draw() const = 0;
virtual ~GraphicalElement() {}
};

class Line : public GraphicalElement {


public:
void draw() const override {
cout << "Drawing a Line." << endl;
}
};

class Circle : public GraphicalElement {


public:
void draw() const override {
cout << "Drawing a Circle." << endl;
}
};

class Rectangle : public GraphicalElement {


public:
void draw() const override {
cout << "Drawing a Rectangle." << endl;
}
};
void drawElements(const vector<GraphicalElement*>& elements) {
for (const auto& element : elements) {
element->draw();
}
}

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>

using namespace std;

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

class Electronics : public Product {


public:
Electronics(const string& id, double price) : Product(id, price) {}
double getPrice() const override {
return price;
}
virtual ~Electronics() {}
};

class Clothing : public Product {


public:
Clothing(const string& id, double price) : Product(id, price) {}
double getPrice() const override {
return price;
}
virtual ~Clothing() {}
};

int main() {
Product* products[2];
products[0] = new Electronics("E123", 299.99);
products[1] = new Clothing("C456", 49.99);

for (int i = 0; i < 2; ++i) {


cout << "Price of product " << (i + 1) << ": $" << products[i]-
>getPrice() << endl;
}
for (int i = 0; i < 2; ++i) {
delete products[i];
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy