Imagine we are writing a program to manage a veterinarian's office. This veterinarian has a limited practice, only treating two kinds of pets: dogs and fish. With this understanding, we identify six classes and the relationships binding them together, as illustrated in the following UML class diagram.
Owner.h | Pet.h |
---|---|
#pragma once;
#include "Address"
#include "Pet.h"
#include <iostream>
#include <string>
using namespace std;
class Pet; // (a)
class Owner
{
private:
string name;
Pet* myPet = nullptr; // (b)
Address home; // (c)
public:
Owner(string n, string s, string c)
: name(n), home(s, c) {}
void setPet(Pet* p) { myPet = p; } // (d)
void display() // (e)
{
cout << "Owner: " << name << endl;
home.display();
if (myPet != nullptr)
myPet->display();
}
}; |
#pragma once;
#include "Owner.h"
#include <iostream>
#include <string>
using namespace std;
class Owner; // (a)
class Pet
{
private:
string name;
Owner* owner = nullptr; // (b)
public:
Pet(string n) : name(n) {}
void setOwner(Owner o) { owner = o; } // (d)
void display() // (e)
{
cout << "Pet: " << name << endl;
}
};
|
this
object.Dog.h | Fish.h |
---|---|
#pragma once;
#include "Pet.h" // (a)
#include "Date.h"
#include <string>
#include <iostream>
using namespace std;
class Dog : public Pet // (b)
{
private:
int akcNum;
Date* shots = nullptr; // (i)
public:
Dog(string name, int akc)
: Pet(name), akcNum(akc) {} // (c)
~Dog() { delete shots; } // (ii)
void setShots(int y, int m, int d) // (iii)
{
if (shots != nullptr)
delete shots;
shots = new Date(y, m, d);
}
void display()
{
Pet::display(); // (d)
cout << "AKC#: " << akcNum << endl;
if (shots != nullptr)
shots->display();
}
}; |
#pragma once; #include "Pet.h" // (a) #include <string> #include <iostream> using namespace std; class Fish : public Pet // (b) { private: int color; public: Fish(string name, int c) : Pet(name), color(c) {} // (c) void display() { Pet::display(); // (d) cout << "Fish color: " << color << endl; } }; |
Address.h | Date.h |
---|---|
#pragma once; #include <string> #include <iostream> using namespace std; class Address { private: string street; string city; public: Address(string s, string c) : street(s), city(c) {} void display() { cout << "Street: " << street << " City: " << city << endl; } }; |
#pragma once; #include <string> #include <iostream> using namespace std; class Date { private: int year; int month; int day; public: Address(int y, int m, int d) : year(m), month(m), day(d) {} void display() { cout << year << "/" << month << "/" << day << endl; } }; |