0% found this document useful (0 votes)
12 views8 pages

OOP

Uploaded by

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

OOP

Uploaded by

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

// OOP review.cpp : This file contains the 'main' function.

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

double getX() const


{
return this->x;
}

double getY() const


{
return this->y;
}

double distanceToPoint(const Point& pointA)


{
double distanceX = pointA.x - this->x;
double distanceY = pointA.y - this->y;
return sqrt(distanceX * distanceX + distanceY * distanceY);
}
};

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

double getX() const


{
return this->x;
}

double getY() const


{
return this->y;
}

double distanceToPoint(const Point& pointA)


{
double distanceX = pointA.x - this->x;
double distanceY = pointA.y - this->y;
return sqrt(distanceX * distanceX + distanceY * distanceY);
}
};

class Circle
{
private:
Point center;
double radius;

public:
Circle()
{

Circle(Point center, double radius)


{
this->center = center;
this->radius = radius;
}

Circle(const Circle& circle)


{
this->center = circle.center;
this->radius = circle.radius;
}

void setCenter(Point point)


{
this->center = point;
}

void setRadius(double radius)


{
this->radius = radius;
}

Point getCenter() const


{
return this->center;
}

double getRadius() const


{
return this->radius;
}

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

// Constructor: set the values of hp, x and y to each parameter


Character(int hp, int x, int y);

// Set and get hp


int getHp();
void setHp(int hp);

// Set and get x


int getX();
void setX(int x);

// Set and get y


int getY();
void setY(int y);

// Get Manhattan distance to other character


int getManhattanDistTo(Character* other);
};
Character::Character()
{
this->hp = 0;
this->x = 0;
this->y = 0;
}

Character::Character(int hp, int x, int y)


{
this->hp = hp;
this->x = x;
this->y = y;
}

int Character::getHp()
{
return this->hp;
}

void Character::setHp(int hp)


{
this->hp = 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;
}

int Character::getManhattanDistTo(Character* other)


{
int distanceX = abs(other->x - this->x);
int distanceY = abs(other->y - this->y);
return distanceX + distanceY;
}

//Q4
#include <cstring>
class Book
{
private:
char* title;
char* authors;
int publishingYear;

public:
Book()
{
this->title = nullptr;
this->authors = nullptr;
this->publishingYear = 0;
}

Book(const char* title, const char* authors, int publishingYear)


{
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
this->authors = new char[strlen(authors) + 1];
strcpy(this->authors, authors);
this->publishingYear = publishingYear;
}

Book(const Book& book)


{
this->title = new char[strlen(book.title) + 1];
strcpy(this->title, book.title);
this->authors = new char[strlen(book.authors) + 1];
strcpy(this->authors, book.authors);
this->publishingYear = book.publishingYear;
}

void setTitle(const char* title)


{
delete[] this->title;
this->title = new char[strlen(title) + 1];
strcpy(this->title, title);
}

void setAuthors(const char* authors)


{
delete[] this->authors;
this->authors = new char[strlen(authors) + 1];
strcpy(this->authors, authors);
}

void setPublishingYear(int publishingYear)


{
this->publishingYear = publishingYear;
}

char* getTitle() const


{
return this->title;
}

char* getAuthors() const


{
return this->authors;
}

int getPublishingYear() const


{
return publishingYear;
}

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

virtual void printType() = 0;


friend class ToyBox;
};

class CarToy : public Toy


{
private:
Color color;

public:
CarToy(double price, Color color) : Toy(price)
{
this->color = color;
}

void printType()
{
cout << "This is a car toy\n";
}
friend class ToyBox;
};

class PuzzleToy : public Toy


{
private:
Size size;

public:
PuzzleToy(double price, Size size) : Toy(price)
{
this->size = size;
}

void printType()
{
cout << "This is a puzzle toy\n";
}

friend class ToyBox;


};

class ToyBox
{
private:
Toy* toyBox[5];
int numberOfItems;

public:
ToyBox()
{
for (int i = 0; i < 5; i++)
{
toyBox[i] = nullptr;
}
numberOfItems = 0;
}

int addItem(const CarToy& carToy)


{
if (numberOfItems == 5)
{
return -1;
}
toyBox[numberOfItems] = new CarToy(carToy);
numberOfItems++;
return numberOfItems;
}

int addItem(const PuzzleToy& puzzleToy)


{
if (numberOfItems == 5)
{
return -1;
}
toyBox[numberOfItems] = new PuzzleToy(puzzleToy);
numberOfItems++;
return numberOfItems;
}
void printBox()
{
for (int i = 0; i < numberOfItems; i++)
toyBox[i]->printType();
}
};

You might also like