C++
Q2/dayOfYear.cpp
Q2/dayOfYear.cpp
/*
Name:
Copyright:
Author:
Date: 17/02/09 15:26
Description:
*/
#include
"dayOfYear.h"
DayOfYear
::
DayOfYear
()
:
day
(
1
),
month
(
1
),
year
(
2009
)
{
/* day =1;
month =1;
year =2009;
*/
}
DayOfYear
::
DayOfYear
(
int
new_day
,
int
new_month
,
int
new_year
)
{
day
=
new_day
;
month
=
new_month
;
year
=
new_year
;
}
void
DayOfYear
::
output
()
const
{
cout
<<
"("
<<
day
<<
" ,"
<<
month
<<
" "
<<
year
<<
")"
<<
endl
;
}
int
DayOfYear
::
get_day
()
{
return
day
;
}
int
DayOfYear
::
get_month
()
{
return
month
;
}
int
DayOfYear
::
get_year
()
{
return
year
;
}
void
DayOfYear
::
set_day
(
int
new_day
)
{
day
=
new_day
;
}
bool
equal
(
DayOfYear
date1
,
DayOfYear
date2
)
{
if
(
(
date1
.
day
==
date2
.
day
)
&&
(
date1
.
month
==
date2
.
month
)
&&
(
date1
.
year
==
date2
.
year
)
)
return
true
;
else
return
false
;
}
Q2/dayOfYear.h
#ifndef _DAYOFYEAR_H #define _DAYOFYEAR_H #include <iostream> using namespace std; class DayOfYear{ public: friend bool equal(DayOfYear date1, DayOfYear date2);//a friend function that compares two objects //yes: if date1 is the same as date2; no: otherwise DayOfYear();//default constructor: 1/1/2009 DayOfYear(int new_day, int new_month, int new_year);// void output() const; // int get_day(); // return the value of "day" int get_month(); int get_year(); void set_DayOfYear(int new_day, int new_month, int new_year); //new_day-->day, new_month->month, new_year-->year void set_day(int new_day); // day-->new_day void set_month(int new_month); void set_year(int new_year); private: int day; int month; //int --> char * int year; }; #endif //
Q2/person.cpp
Q2/person.cpp
/*
Name: person.cpp
Copyright:
Author:
Date: 22/02/09 14:35
Description: declaration of a new class DayOfYear
*/
#include
"person.h"
Person
::~
Person
()
{
if
(
emails
!=
NULL
){
delete
[]
emails
;
emails
=
NULL
;
}
}
Person
::
Person
()
{
id
=
-
1
;
name
=
"NA"
;
//birthday = DayOfYear( 1,1, 2000);
emails
=
NULL
;
numEmails
=
0
;
}
Person
::
Person
(
int
new_id
,
string new_name
,
DayOfYear
date
)
{
id
=
new_id
;
name
=
new_name
;
//birthday = date;
emails
=
NULL
;
numEmails
=
0
;
}
/**/
Person
::
Person
(
const
Person
&
someone
)
{
id
=
someone
.
id
;
name
=
someone
.
name
;
birthday
=
someone
.
birthday
;
numEmails
=
someone
.
numEmails
;
//allocate space to *emails if numEmails>0
if
(
numEmails
==
0
)
emails
=
NULL
;
else
{
emails
=
new
string
[
numEmails
];
for
(
int
i
=
0
;
i
<
numEmails
;
i
++
)
emails
[
i
]
=
someone
.
emails
[
i
];
}
}
/**/
/**/
void
Person
::
operator
=
(
const
Person
&
rhs
)
{
id
=
rhs
.
id
;
name
=
rhs
.
name
;
birthday
=
rhs
.
birthday
;
//allocate space to *emails if needed
if
(
numEmails
>
0
)
delete
[]
emails
;
//release the old memory
numEmails
=
rhs
.
numEmails
;
emails
=
new
string
[
numEmails
];
for
(
int
i
=
0
;
i
<
numEmails
;
i
++
)
emails
[
i
]
=
rhs
.
emails
[
i
];
}
/**/
void
Person
::
output
()
const
{
// id = 200;
cout
<<
"--------------------------\n"
;
cout
<<
" id="
<<
(
*
this
).
id
<<
" name="
<<
this
->
name
<<
" birthday="
;
birthday
.
output
();
for
(
int
i
=
0
;
i
<
numEmails
;
i
++
)
cout
<<
"email-#"
<<
i
<<
": "
<<
emails
[
i
]
;
cout
<<
"\n--------------------------\n\n"
;
}
int
Person
::
get_id
()
const
{
return
id
;
}
string
Person
::
get_name
()
const
{
return
name
;
}
const
DayOfYear
Person
::
get_birthday
()
const
{
return
birthday
;
}
void
Person
::
set_id
(
int
new_id
)
{
id
=
new_id
;
}
void
Person
::
set_name
(
string new_name
)
{
name
=
new_name
;
}
void
Person
::
set_birthday
(
DayOfYear
date
)
{
birthday
=
date
;
}
string
Person
::
getEmail
(
int
i
)
const
{
if
(
i
>=
0
&&
i
<
numEmails
)
return
emails
[
i
];
else
return
"NA"
;
}
void
Person
::
add_email
(
string the_email
)
{
//case 1: emails list is empty
if
(
numEmails
==
0
){
emails
=
new
string
[
1
];
//verification
emails
[
0
]
=
the_email
;
}
else
{
//case 2: expand the list
string
*
tmp_emails
=
new
string
[
numEmails
];
for
(
int
i
=
0
;
i
<
numEmails
;
i
++
)
//save the emails
tmp_emails
[
i
]
=
emails
[
i
];
delete
[]
emails
;
emails
=
new
string
[
numEmails
+
1
];
//expand the list by one
//verify the above allocation
for
(
int
i
=
0
;
i
<
numEmails
;
i
++
)
emails
[
i
]
=
tmp_emails
[
i
];
//copy the existing emails
emails
[
numEmails
]
=
the_email
;
// add the new email
delete
[]
tmp_emails
;
}
numEmails
+=
1
;
}
int
Person
::
get_num_emails
()
const
{
return
numEmails
;
}
void
print
(
const
Person
&
someone
)
{
cout
<<
"****"
<<
someone
.
get_id
()
<<
" "
<<
someone
.
get_name
()
<<
endl
;
}
//perform a sequential search on the studentList
bool
searchStudent
(
Person
*
studentList
,
int
num_students
,
int
key
)
{
for
(
int
i
=
0
;
i
<
num_students
;
i
++
){
if
(
studentList
[
i
].
get_id
()
==
key
)
//found
return
true
;
}
//for (i)
return
false
;
//none of the students has ID=key
}
bool
sameID
(
const
Person
&
person1
,
const
Person
&
person2
)
{
return
(
person1
.
get_id
()
==
person2
.
get_id
());
}
bool
equalID
(
const
Person
&
person1
,
const
Person
&
person2
)
{
return
(
person1
.
id
==
person2
.
id
);
}
Q2/person.h
//person.h #ifndef _PERSON_H #define _PERSON_H #include <iostream> #include <string> #include "dayOfYear.h" using namespace std; class Person{ public: //equalID() returns true:have the same ID; false: otherwise friend bool equalID(const Person & person1, const Person &Person2);// ~Person(); //destructor: to release any memory that has been allocated to the object Person(); //default constructor: id=-1, name="NA", birthday="1/1/2000" Person( int new_id, string new_name, DayOfYear date); //constructor that initializes //id to new_id, name to new_name, and birthday to date Person(const Person & someone); //copy constructor: construct a new object as a copy of "someone" void operator= (const Person &rhs); //assign the object on the right-hand-side to the left-hand-side virtual void output() const; //print out a person's info. int get_id() const; //return the id of a Person object string get_name() const; //return the name of a Person object const DayOfYear get_birthday() const; //return the birthday of a Person object string getEmail( int i) const; //return the i-th email if exists; o.w: return "NA" void set_id(int new_id); //change a person's id to new_id void set_name(string new_name); // change a person's name to new_name void set_birthday(DayOfYear new_Date); //change a person's birthday to new_date void add_email( string the_email); //add the_email to the list int get_num_emails() const; private: int id; string name; DayOfYear birthday; string *emails; //list of email addresses int numEmails; }; void print( const Person& someone); //print out a person's id and name bool sameID(const Person& person1, const Person& person2);//true:have the same ID; false: otherwise //search whether there exists a student on the studentList with id=key bool searchStudent( Person *studentList, int num_students, int key); #endif
Q2/personnel.cpp
Q2/personnel.cpp
/*
Name: personnel.cpp
Copyright:
Author:
Date: 22/02/09 15:02
Description: manage a personnel database that currently consists of one employee Mary
*/
#include
"dayOfYear.h"
#include
"person.h"
#include
"student.h"
int
main
()
{
DayOfYear
date
(
20
,
7
,
1985
);
Person
mary_p
(
101
,
"Mary"
,
date
);
Student
mary_s
(
101
,
"Mary"
,
date
,
1
),
mary_c
;
// Person & mary_p = mary_s;
Person
&
person1
=
mary_s
;
mary_s
.
output
();
//student verion
person1
.
output
();
/*
mary_p.output();
mary_s.Person::output();//person version
print( mary_s );
mary_s.add_email("[email protected]");
mary_s.set_num_grades(2);
mary_s.set_grade(1, 98.00);
mary_s.set_grade(2, 100.00);
mary_c = mary_s;
cout << mary_s;
cout << mary_c;
*/
//mary_clone.set_id(202);
// print(mary_clone);
//mary_clone.output();
// mary = mary_clone;
// mary.output();
//sameID(mary, mary_clone);
// equalID(mary, mary_clone);
/**the code below create a dynamic array of Person
***
//1 & 2:
Person *students213 = NULL; //
int num = 200; //size of the dynamic array *students213
//3. allocate memory to students213
students213 = new Person [ num ];
//4. verify
if (students213 == NULL ){
cerr << "Memory allocation failure.\n";
exit( -1 );
}
//5: use
for (int i=0; i<num; i++){
students213[ i ].set_id( i + 100);
students213[ i ].output();
}
//search based on id
cout << "Is there a student with ID=207? ";
if (searchStudent( students213, num, 207) )
cout << " Yes."<<endl;
else
cout << " No. "<<endl;
cout << "Is there a student with ID=507? ";
if (searchStudent( students213, num, 507) )
cout << " Yes."<<endl;
else
cout << " No. "<<endl;
//6&7: release the memory
delete [] students213;
students213 = NULL;
*****
**end of the dynamic array students213
**************************************/
return
0
;
}
Q2/student.cpp
Q2/student.cpp
//File name--student.cpp: implementation file for the Student class
#include
"student.h"
//#include "dayOfYear.h"
//destructor
Student
::~
Student
()
{
if
(
grades
!=
NULL
){
delete
[]
grades
;
}
}
//default constructor
Student
::
Student
()
:
Person
()
{
//call the constructor of the base class to initialize the inherited members
level
=
1
;
//freshmen
num_grades
=
0
;
grades
=
NULL
;
}
//a second constructor
Student
::
Student
(
int
new_id
,
string new_name
,
DayOfYear
date
,
int
lvl
)
:
Person
(
new_id
,
new_name
,
date
),
level
(
lvl
),
num_grades
(
0
),
grades
(
NULL
)
{
//call the corresponding constructor in the
//base class to initialize the inherited members
/* level = lvl;
num_grades = 0;
grades = NULL;
*/
}
//copy constructor
Student
::
Student
(
const
Student
&
std
)
:
Person
(
std
)
//call the copy constructor of the base class
{
level
=
std
.
level
;
num_grades
=
std
.
num_grades
;
if
(
num_grades
<=
0
)
grades
=
NULL
;
else
{
//allocate space to *grades and copy the grades from std
(
*
this
).
grades
=
new
double
[
num_grades
];
if
(
grades
==
NULL
){
cerr
<<
"Student:Student(const Student &): Memory allocation error\n"
;
exit
(
-
1
);
}
for
(
int
i
=
0
;
i
<
num_grades
;
i
++
)
grades
[
i
]
=
std
.
grades
[
i
];
}
}
void
Student
::
operator
=
(
const
Student
&
rhs
)
{
(
*
this
).
Person
::
operator
=
(
rhs
);
level
=
rhs
.
level
;
if
(
num_grades
!=
rhs
.
num_grades
){
delete
[]
grades
;
num_grades
=
rhs
.
num_grades
;
grades
=
new
double
[
num_grades
];
if
(
grades
==
NULL
){
cerr
<<
"Student:operator=(const Student &): Memory allocation error\n"
;
exit
(
-
1
);
}
for
(
int
i
=
0
;
i
<
num_grades
;
i
++
)
grades
[
i
]
=
rhs
.
grades
[
i
];
}
}
int
Student
::
get_level
()
const
{
return
level
;
}
int
Student
::
get_num_grades
()
const
{
return
num_grades
;
}
double
Student
::
get_grade
(
int
i
)
const
{
if
(
i
<
1
||
i
>
num_grades
)
return
-
1.0
;
//non-existent grade
else
return
grades
[
i
-
1
];
}
void
Student
::
set_level
(
int
lvl
)
{
level
=
lvl
;
}
void
Student
::
set_num_grades
(
int
num
)
{
num_grades
=
num
;
if
(
num
>
0
){
grades
=
new
double
[
num
];
if
(
grades
==
NULL
){
cerr
<<
"Student:set_num_grades(int ): Memory allocation error\n"
;
exit
(
-
1
);
}
for
(
int
i
=
0
;
i
<
num
;
i
++
)
grades
[
i
]
=
0
;
}
}
void
Student
::
set_grade
(
int
i
,
double
grd
)
{
if
(
i
>=
1
&&
i
<=
num_grades
)
grades
[
i
-
1
]
=
grd
;
}
ostream
&
operator
<<
(
ostream
&
out
,
const
Student
&
std
)
{
DayOfYear
bday
;
bday
=
std
.
get_birthday
();
out
<<
"------------------------------\n"
;
out
<<
"id="
<<
std
.
get_id
()
<<
endl
;
out
<<
"name="
<<
std
.
get_name
()
<<
endl
;
out
<<
"birthday="
<<
bday
.
get_day
();
out
<<
"-"
<<
bday
.
get_month
();
out
<<
"-"
<<
bday
.
get_year
()
<<
endl
;
out
<<
"#emails="
<<
std
.
get_num_emails
()
<<
endl
;
out
<<
" 1st email="
<<
std
.
getEmail
(
0
)
<<
endl
;
out
<<
"level="
<<
std
.
level
<<
endl
;
out
<<
"#grades="
<<
std
.
num_grades
<<
endl
;
for
(
int
i
=
1
;
i
<=
std
.
num_grades
;
i
++
)
out
<<
"grade["
<<
i
<<
"]="
<<
std
.
grades
[
i
-
1
]
<<
" "
;
out
<<
endl
;
out
<<
"------------------------------\n"
;
return
out
;
}
void
Student
::
output
()
const
{
DayOfYear
bday
;
bday
=
get_birthday
();
cout
<<
"------------------------------\n"
;
cout
<<
"id="
<<
get_id
()
<<
endl
;
cout
<<
"name="
<<
get_name
()
<<
endl
;
//private-is-private rule
cout
<<
"birthday="
<<
bday
.
get_day
();
cout
<<
"-"
<<
bday
.
get_month
();
cout
<<
"-"
<<
bday
.
get_year
()
<<
endl
;
cout
<<
"#emails="
<<
get_num_emails
()
<<
endl
;
cout
<<
" 1st email="
<<
getEmail
(
0
)
<<
endl
;
cout
<<
"level="
<<
level
<<
endl
;
cout
<<
"#grades="
<<
num_grades
<<
endl
;
for
(
int
i
=
1
;
i
<=
num_grades
;
i
++
)
cout
<<
"grade["
<<
i
<<
"]="
<<
grades
[
i
-
1
]
<<
" "
;
cout
<<
endl
;
cout
<<
"------------------------------\n"
;
}
Q2/student.h
//File name--student.h: declare Student as a derived class of Person #ifndef _STUDENT_H #define _STUDENT_H #include "person.h" class Student:public Person { public: ~Student(); //destructor Student(); //default: 1->level, 0->num_grades, NULL->grades Student(int new_id, string new_name, DayOfYear date, int lvl ); //lvl->level, 0->num_grades, NULL->grades Student(const Student& std); //copy constructor: std -->*this void operator=(const Student& rhs); //rhs --> *this int get_level() const; //return level int get_num_grades() const; //return num_grades double get_grade(int i) const; //return grades[i] void set_level(int lvl); //lvl-->level void set_num_grades( int num); //num->num_grades, allocate memory to *grades void set_grade(int i, double grd); //grd --> grades[i] friend ostream & operator <<( ostream & out, const Student& std); virtual void output() const; //print out a student's info. private: int level; //1-4: freshmen, sophomore, junior and senior int num_grades; double *grades; }; #endif //_STUDENT_H
Q1/9.cpp
#include <iostream> #include "figure.h" #include "rectangle.h" #include "triangle.h" using std::cout; int main() { Triangle tri; tri.draw(); cout << "\nDerived class Triangle object calling" << " center().\n"; tri.center(); Rectangle rect; rect.draw(); cout << "\nDerived class Rectangle object calling" << " center().\n"; rect.center(); return 0; }