1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
#include <string.h>
#include <iostream>
using namespace std;
const int MAX_LEN_OF_NAME = 100;
class Person
{
char* m_fname;
char* m_lname;
int m_age;
public:
Person(): m_fname(NULL), m_lname(NULL){}
~Person()
{
delete[] m_fname;
delete[] m_lname;
m_fname = m_lname = NULL;
}
Person(const char* strFirstName, const char* strLastname, int iAge) : m_age(iAge), m_fname(NULL), m_lname(NULL)
{
SetName(strFirstName, strLastname);
}
//Set name of the person
void SetName(const char* strFirstName, const char* strLastname)
{
//Don't reallocate memory if the length of the new name is less than the current one
if(m_fname == NULL || strlen(m_fname) < strlen(strFirstName))
{
delete[] m_fname;
m_fname = new char[strlen(strFirstName) + 1];
}
strcpy(m_fname, strFirstName);
if(m_lname == NULL || strlen(m_lname) < strlen(strLastname))
{
delete[] m_lname;
m_lname = new char[strlen(strLastname) + 1];
}
strcpy(m_lname, strLastname);
}
void SetAge(int iAge)
{
m_age = iAge;
}
const char* GetFirstName() const
{
return m_fname;
}
const char* GetLastName() const
{
return m_lname;
}
int GetAge() const
{
return m_age;
}
};
class Movie
{
char* m_name;
public:
Movie() : m_name(NULL)
{
}
~Movie()
{
delete[] m_name;
m_name = NULL;
}
void SetName(const char* strName)
{
if(m_name == NULL || strlen(m_name) < strlen(strName))
{
delete[] m_name;
m_name = new char[strlen(strName) + 1];
}
strcpy(m_name, strName);
}
const char* GetName() const
{
return m_name;
}
};
class Director : public Person
{
Movie* m_movies;
int m_numOfMovies;
public:
Director(const char* strFirstName, const char* strLastname, int iAge) : m_numOfMovies(0), m_movies(NULL)
{
//Set name of the director
SetName(strFirstName, strLastname);
SetAge(iAge);
}
~Director()
{
delete[] m_movies;
m_movies = NULL;
}
//Allocate memory for specific number of movies
void SetNumOfMovies(int iNum)
{
if(m_numOfMovies == iNum)
return;
m_numOfMovies = iNum;
if(m_numOfMovies > 0)
{
delete[] m_movies;
m_movies = new Movie[m_numOfMovies];
}
}
void SetMovies()
{
int iNumOfMovies;
cout << "How many movies he produced?" << endl;
cin >> iNumOfMovies;
SetNumOfMovies(iNumOfMovies);
char movieName[MAX_LEN_OF_NAME];
for(int i = 0; i < m_numOfMovies; i++)
{
cout << "What the name of the " << i + 1 << " movie?" << endl;
cin >> movieName;
m_movies[i].SetName(movieName);
}
}
const Movie* GetMovies() const
{
return m_movies;
}
int GetNumOfMovies() const
{
return m_numOfMovies;
}
};
class Actor : public Person
{
Movie* m_movies;
int m_numOfMovies;
public:
Actor(const char* strFirstName, const char* strLastname, int iAge) : m_numOfMovies(0), m_movies(NULL)
{
//Set name of the director
SetName(strFirstName, strLastname);
SetAge(iAge);
}
~Actor()
{
delete[] m_movies;
m_movies = NULL;
}
//Allocate memory for specific number of movies
void SetNumOfMovies(int iNum)
{
if(m_numOfMovies == iNum)
return;
m_numOfMovies = iNum;
if(m_numOfMovies > 0)
{
delete[] m_movies;
m_movies = new Movie[m_numOfMovies];
}
}
void SetMovies()
{
int iNumOfMovies;
cout << "In how many movies he played?" << endl;
cin >> iNumOfMovies;
SetNumOfMovies(iNumOfMovies);
char movieName[MAX_LEN_OF_NAME];
for(int i = 0; i < m_numOfMovies; i++)
{
cout << "What the name of the " << i + 1 << " movie" << endl;
cin >> movieName;
m_movies[i].SetName(movieName);
}
}
const Movie* GetMovies() const
{
return m_movies;
}
int GetNumOfMovies() const
{
return m_numOfMovies;
}
};
//Class for person who is Director and Actor
class DirActor : public Person
{
Director* m_director;
Actor* m_actor;
public:
DirActor(const char* strFirstName, const char* strLastname, int iAge)
{
SetName(strFirstName, strLastname);
SetAge(iAge);
m_director = new Director(strFirstName, strLastname,iAge);
m_actor = new Actor(strFirstName, strLastname,iAge);
}
~DirActor()
{
delete m_director;
m_director = NULL;
delete m_actor;
m_actor = NULL;
}
Director* GetDirector() const
{
return m_director;
}
Actor* GetActor() const
{
return m_actor;
}
void PrintDetails()
{
cout << "=============================================================================" << endl;
cout << "Printing information about " << GetFirstName() << " " << GetLastName() << endl;
cout << "=============================================================================" << endl;
cout << "Age:" << GetAge() << endl << endl;
cout << "The movies he produced are:" << endl;
for(int i = 0; i < m_director->GetNumOfMovies(); i++)
{
cout << i + 1 << " movie:" << m_director->GetMovies()[i].GetName() << endl;
}
cout << endl;
cout << "The movies he played in are:" << endl;
for(int i = 0; i < m_actor->GetNumOfMovies(); i++)
{
cout << i + 1 << " movie:" << m_actor->GetMovies()[i].GetName() << endl;
}
}
};
void main()
{
DirActor da("Clint", "Eastwood", 79);
da.GetDirector()->SetMovies();
da.GetActor()->SetMovies();
da.PrintDetails();
}
| |