OOP
OOP
Program execution
begins and ends there.
//
#include <cmath>
//Q1
class Point
{
private:
double x, y;
public:
Point()
{
x = 0.0;
y = 0.0;
}
Point(double x, double y)
{
this->x = x;
this->y = y;
}
void setX(double x)
{
this->x = x;
}
void setY(double y)
{
this->y = y;
}
//Q2
class Point
{
private:
double x, y;
public:
Point()
{
x = 0.0;
y = 0.0;
}
Point(double x, double y)
{
this->x = x;
this->y = y;
}
void setX(double x)
{
this->x = x;
}
void setY(double y)
{
this->y = y;
}
class Circle
{
private:
Point center;
double radius;
public:
Circle()
{
void printCircle()
{
printf("Center: {%.2f, %.2f} and Radius %.2f\n", this->center.getX(), this-
>center.getY(), this->radius);
}
};
//Q3
class Character {
protected:
int hp;
int x;
int y;
public:
// Constructor: set the values of x and y and hp to 0
Character();
int Character::getHp()
{
return this->hp;
}
int Character::getX()
{
return this->x;
}
void Character::setX(int x)
{
this->x = x;
}
int Character::getY()
{
return this->y;
}
void Character::setY(int y)
{
this->y = y;
}
//Q4
#include <cstring>
class Book
{
private:
char* title;
char* authors;
int publishingYear;
public:
Book()
{
this->title = nullptr;
this->authors = nullptr;
this->publishingYear = 0;
}
~Book()
{
delete[] title;
delete[] authors;
}
void printBook() {
printf("%s\n%s\n%d", this->title, this->authors, this->publishingYear);
}
};
//Q5
enum Color
{
red,
green,
blue
};
enum Size
{
small,
medium,
big
};
class Toy
{
protected:
double price;
public:
Toy(double price)
{
this->price = price;
}
public:
CarToy(double price, Color color) : Toy(price)
{
this->color = color;
}
void printType()
{
cout << "This is a car toy\n";
}
friend class ToyBox;
};
public:
PuzzleToy(double price, Size size) : Toy(price)
{
this->size = size;
}
void printType()
{
cout << "This is a puzzle toy\n";
}
class ToyBox
{
private:
Toy* toyBox[5];
int numberOfItems;
public:
ToyBox()
{
for (int i = 0; i < 5; i++)
{
toyBox[i] = nullptr;
}
numberOfItems = 0;
}