第一题
#include <iostream>
#include <string>
using namespace std;
class Vehicle
{
public:
Vehicle(int doors, int cylinders, string _color, double fuel, int transmission)
{
numberOfDoors = doors;
numberOfCylinders = cylinders;
color = _color;
fuelLevel = fuel;
transmissionType = transmission;
className = "Vehicle";
}
int getNumberOfDoors()
{
return numberOfDoors;
}
void setNumberOfDoors(int _numberOfDoors)
{
numberOfDoors = _numberOfDoors;
}
int getNumberOfCylinders()
{
return numberOfCylinders;
}
void setNumberOfCylinders(int _numberOfCylinders)
{
numberOfCylinders = _numberOfCylinders;
}
string getColor()
{
return color;
}
void setColor(string _color)
{
color = _color;
}
double getFuelLevel()
{
return fuelLevel;
}
void setFuelLevel(double _fuelLevel)
{
fuelLevel = _fuelLevel;
}
string getTransmissionType()
{
if (transmissionType == 1)
return "Automatic";
else
return "Manual";
}
void setTransmissionType(int _transmissionType)
{
transmissionType = _transmissionType;
}
string getClassName()
{
return className;
}
void setClassName(string _className)
{
className = _className;
}
friend ostream& operator << (ostream& out, Vehicle v)
{
out << v.getClassName() << endl;
out << "Number of doors:" << v.getNumberOfDoors() << endl;
out << "Number of cylinders:" << v.getNumberOfCylinders() << endl;
out << "Transmission type:" << v.getTransmissionType() << endl;
out << "Color:" << v.getColor() << endl;
out << "Fuel level:" << v.getFuelLevel() << endl;
return out;
}
private:
int numberOfDoors;
int numberOfCylinders;
string color;
double fuelLevel;
int transmissionType;
string className;
};
class Taxi :public Vehicle
{
public:
Taxi(int doors, int cylinders, string _color, double fuel, int transmission, bool _customers)
:Vehicle(doors, cylinders, _color, fuel, transmission)
{
customers = _customers;
setClassName("Taxi");
}
bool getCustomers()
{
return customers;
}
void setCustomers(bool _customers)
{
customers = _customers;
}
friend ostream& operator << (ostream& out, Taxi v)
{
out << v.getClassName() << endl;
out << "Number of doors:" << v.getNumberOfDoors() << endl;
out << "Number of cylinders:" << v.getNumberOfCylinders() << endl;
out << "Transmission type:" << v.getTransmissionType() << endl;
out << "Color:" << v.getColor() << endl;
out << "Fuel level:" << v.getFuelLevel() << endl;
if (v.getCustomers() == 0)out << "Has no passengers" << endl;
return out;
}
private:
bool customers;
};
class Truck:public Vehicle
{
public:
Truck(int doors, int cylinders, string _color, double fuel, int transmission, bool _cargo)
:Vehicle(doors, cylinders, _color, fuel, transmission)
{
cargo = _cargo;
setClassName("Truck");
}
bool getCargo()
{
return cargo;
}
void setCargo(bool _cargo)
{
cargo = _cargo;
}
friend ostream& operator << (ostream& out, Truck v)
{
out << v.getClassName() << endl;
out << "Number of doors:" << v.getNumberOfDoors() << endl;
out << "Number of cylinders:" << v.getNumberOfCylinders() << endl;
out << "Transmission type:" << v.getTransmissionType() << endl;
out << "Color:" << v.getColor() << endl;
out << "Fuel level:" << v.getFuelLevel() << endl;
if (v.getCargo() == 1)out << "Is carrying cargo" << endl;
return out;
}
private:
bool cargo;
};
int main()
{
int doors, cylinders, transmission;
string color;
double fuel;
cin >> doors >> cylinders >> color >> fuel >> transmission;
Vehicle vehicle(doors, cylinders, color, fuel, transmission);
cout << vehicle;
cin >> doors >> cylinders >> color >> fuel >> transmission;
Taxi taxi(doors, cylinders, color, fuel, transmission, false);
cout << taxi;
cin >> doors >> cylinders >> color >> fuel >> transmission;
Truck truck(doors, cylinders, color, fuel, transmission, true);
cout << truck;
return 0;
}
第二题
#include <iostream>
#include <string>
using namespace std;
enum status_t { FRESHMAN, SOPHOMORE, JUNIOR, SENIOR };
string status_str[] = { "Freshman", "Sophomore", "Junior", "Senior" };
enum rank_t { PROFESSOR, ASSOCIATE_PROFESSOR, LECTURER };
string ranke_str[] = { "Professor", "Associate professor", "Lecturer" };
enum headship_t { PRESIDENT, DEAN, DEPARTMENT_CHAIRMAN };
string headship_str[] = { "President", "Dean", "Department chairman" };
class Person
{
public:
Person(string _name, char _sex)
{
name = _name;
sex = _sex;
}
string getName()
{
return name;
}
void setName(string _name)
{
name = _name;
}
char getSex()
{
return sex;
}
void setSex(char _sex)
{
sex = _sex;
}
void print()
{
cout << "Name:" << name << ", Sex:" << sex << endl;
}
protected:
string name;
char sex;
private:
};
class Student :public Person
{
public:
Student(string _name, char _sex, status_t _status)
:Person(_name, _sex)
{
status = _status;
}
void print()
{
Person::print();
cout << "Status:" << status_str[status] << endl;
}
private:
status_t status;
};
class MyDate
{
public:
MyDate(int _year = 1990, int _month = 1, int _day = 1)
{
year = _year;
month = _month;
day = _day;
}
void print()
{
cout << year << "-" << month << "-" << day;
}
private:
int year;
int month;
int day;
};
class Employee :public Person
{
public:
Employee(string _name, char _sex, int _salary, MyDate _dateHired)
:Person(_name, _sex)
{
salary = _salary;
dateHired = _dateHired;
}
void print()
{
Person::print();
cout << "Salary:" << salary << ", Hire date:";
dateHired.print();
cout << endl;
}
protected:
int salary;
MyDate dateHired;
private:
};
class Faculty :public Employee
{
public:
Faculty(string _name, char _sex, int _salary, MyDate _dateHired, rank_t _rank)
:Employee(_name, _sex, _salary, _dateHired)
{
name = _name;
sex = _sex;
salary = _salary;
dateHired = _dateHired;
rank = _rank;
}
void print()
{
Employee::print();
cout << "Rank:" << ranke_str[rank] << endl;
}
private:
rank_t rank;
};
class Staff :public Employee
{
public:
Staff(string _name, char _sex, int _salary, MyDate _dateHired, headship_t _headship)
:Employee(_name, _sex, _salary, _dateHired)
{
headship = _headship;
}
void print()
{
Employee::print();
cout << "Headship:" << headship_str[headship] << endl;
}
private:
headship_t headship;
};
int main()
{
Person person("ZhangSan", 'M');
Student student("LiSi", 'F', FRESHMAN);
MyDate date(2012, 3, 1);
Employee employee("WangWu", 'M', 5000, date);
Faculty faculty("LiuLiu", 'M', 10000, date, PROFESSOR);
Staff staff("QianQi", 'M', 8000, date, DEPARTMENT_CHAIRMAN);
person.print();
student.print();
employee.print();
faculty.print();
staff.print();
return 0;
}
第三题
#include <iostream>
using namespace std;
class C
{
public:
C()
{
cout << "C()" << endl;
}
~C()
{
cout << "~C()" << endl;
}
};
class B :public C
{
public:
B()
{
cout << "B()" << endl;
}
~B()
{
cout << "~B()" << endl;
}
};
class A :public C
{
public:
A()
{
cout << "A()" << endl;
}
~A()
{
cout << "~A()" << endl;
}
};
class D :public B, public A
{
public:
D()
{
cout << "D()" << endl;
}
~D()
{
cout << "~D()" << endl;
}
};
class E :public D
{
public:
E()
{
cout << "E()" << endl;
}
~E()
{
cout << "~E()" << endl;
}
};
int main()
{
E e;
return 0;
}
第四题
#include <iostream>
using namespace std;
class Point
{
public:
Point(int _x = 0, int _y = 0)
{
x = _x;
y = _y;
}
int getX()
{
return x;
}
void setX(int _x)
{
x = _x;
}
int getY()
{
return y;
}
void setY(int _y)
{
y = _y;
}
void print()
{
cout << "(" << x << ", " << y << ")";
}
private:
int x;
int y;
};
class Arc
{
public:
Arc(Point _p, double _radius = 0)
{
p = _p;
radius = _radius;
}
Point getP()
{
return p;
}
void setP(Point _p)
{
p = _p;
}
double getRadius()
{
return radius;
}
void setRadius(double _radius)
{
radius = _radius;
}
void draw()
{
cout << "Drawing an arc: Center";
p.print();
cout << ", radius(" << radius << ")" << endl;
}
private:
Point p;
double radius;
};
class Circle
{
public:
Circle(Point _p, double _radius = 0)
{
p = _p;
radius = _radius;
}
Point getP()
{
return p;
}
void setP(Point _p)
{
p = _p;
}
double getRadius()
{
return radius;
}
void setRadius(double _radius)
{
radius = _radius;
}
void draw()
{
cout << "Drawing an arc: Center";
p.print();
cout << ", radius(" << radius << ")" << endl;
}
private:
Point p;
double radius;
};
class Ellipse
{
public:
Ellipse(Point _p, double _xRadius = 0, double _yRadius = 0)
{
p = _p;
xRadius = _xRadius;
yRadius = _yRadius;
}
Point getP()
{
return p;
}
void setP(Point _p)
{
p = _p;
}
double getXRadius()
{
return xRadius;
}
void setXRadius(double _xRadius)
{
xRadius = _xRadius;
}
double getYRadius()
{
return yRadius;
}
void setYRadius(double _yRadius)
{
yRadius = _yRadius;
}
void draw()
{
cout << "Drawing an ellipse: Center";
p.print();
cout << ", x-axis(" << xRadius;
cout << "), y-axis(" << yRadius << ")" << endl;
}
private:
Point p;
double xRadius;
double yRadius;
};
class Rectangle
{
public:
Rectangle(Point _p, double _width = 0, double _height = 0)
{
p = _p;
width = _width;
height = _height;
}
Point getP()
{
return p;
}
void setP(Point _p)
{
p = _p;
}
double getWidth()
{
return width;
}
void setWidth(double _width)
{
width = _width;
}
double getHeight()
{
return height;
}
void setHeight(double _height)
{
height = _height;
}
void draw()
{
cout << "Drawing a rectangle: Upper left corner coordinates";
p.print();
cout << ", width(" << width;
cout << "), height(" << height << ")" << endl;
}
private:
Point p;
double width;
double height;
};
class Mix
{
public:
Mix(Point _p, double _a, double _b)
{
p = _p;
a = _a;
b = _b;
}
void draw()
{
Arc arc(p, a);
Circle cir(p, b);
Ellipse elp(p, a, b);
Rectangle rec(p, a, b);
arc.draw();
cir.draw();
elp.draw();
rec.draw();
}
private:
Point p;
double a;
double b;
};
int main() {
Point p(320, 240);
Mix mix(p, 100, 70);
mix.draw();
return 0;
}
第五题
#include <iostream>
#include <string>
using namespace std;
class A
{
private:
int n;
public:
A(int x)
{
n = x;
}
A& operator ++()
{
n++;
return *this;
}
A operator ++(int)
{
A t = *this;
++*this;
return t;
}
friend ostream& operator<<(ostream& out, A a)
{
out << a.n;
return out;
}
operator int()
{
return n;
}
};
class B
{
private:
static int k;
static int Square(int n)
{
cout << "k=" << k << endl;
return n * n;
}
friend int main();
};
int B::k;
int main()
{
A a1(1), a2(2);
cout << a1++ << endl;
(++a1) = 100;
cout << a1 << endl;
cout << ++a1 << endl;
cout << a1 << endl;
int n;
cin >> n;
while (n--)
{
cin >> B::k;
A a3(B::k);
cout << B::Square(a3) << endl;
}
return 0;
}
第六题
#include <iostream>
using namespace std;
class C
{
public:
C() {}
virtual void print() = 0;
private:
};
class A :public C
{
public:
int k;
A(int _k)
{
k = _k;
}
virtual void print()
{
cout << "A " << k << endl;
}
private:
};
class B :public C
{
public:
int k;
B(int _k)
{
k = _k;
}
virtual void print()
{
cout << "B " << k << endl;
}
private:
};
C* a[100];
void PrintInfo(C* obj)
{
obj->print();
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; ++i)
{
char c;
int k;
cin >> c >> k;
if (c == 'A')
a[i] = new A(k);
else
a[i] = new B(k);
}
cout << n << endl;
for (int i = 0; i < n; ++i)
PrintInfo(a[i]);
cout << "****" << endl;
}
}
第七题
#include <iostream>
using namespace std;
class A
{
public:
A()
{
cout << "A constructor" << endl;
g();
}
virtual ~A()
{
cout << "A destructor" << endl;
}
void g() {
fun();
}
virtual void fun()
{
cout << "Call class A's fun" << endl;
}
};
class B :public A
{
public:
B()
{
cout << "B constructor" << endl;
}
virtual ~B()
{
cout << "B destructor" << endl;
}
};
class C :public B
{
public:
C()
{
cout << "C constructor" << endl;
}
virtual ~C()
{
cout << "C destructor" << endl;
}
virtual void fun()
{
cout << "Call class C's fun" << endl;
}
};
int main()
{
A *a = new C;
a->g();
delete a;
return 0;
}
第八题
#include <iostream>
using namespace std;
class A
{
public:
virtual ~A()
{
cout << "A::~A() called" << endl;
}
virtual void print()
{
cout << "print come form class A" << endl;
}
};
class B : public A
{
private:
char *buf;
public:
B(int) {}
virtual ~B()
{
cout << "B::~B() called" << endl;
}
virtual void print()
{
cout << "print come from class B" << endl;
}
};
void fun(A *a)
{
delete a;
}
int main()
{
A *a = new B(10);
a->print();
fun(a);
B *b = new B(20);
fun(b);
return 0;
}
第九题
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double PI = acos(-1.0);
class Shape
{
public:
virtual double getArea() = 0;
virtual double getPerimeter() = 0;
virtual void show() = 0;
virtual string getClassName()
{
return "Shape";
}
};
class Circle :public Shape
{
public:
Circle(double _radius = 1)
{
radius = _radius;
}
double getRadius()
{
return radius;
}
void setRadius(double _radius)
{
radius = _radius;
}
virtual double getArea()
{
return PI*radius*radius;
}
virtual double getPerimeter()
{
return 2 * PI*radius;
}
virtual void show()
{
cout << "Radius:" << radius << endl;
}
virtual string getClassName()
{
return "Circle";
}
private:
double radius;
};
class Rectangle :public Shape
{
public:
Rectangle(double _width, double _height)
{
width = _width;
height = _height;
}
double getWidth()
{
return width;
}
void setWidth(double _width)
{
width = _width;
}
double getHeight()
{
return height;
}
void setHeight(double _height)
{
height = _height;
}
virtual double getArea()
{
return width*height;
}
virtual double getPerimeter()
{
return (width + height) * 2;
}
virtual void show()
{
cout << "Width:" << width << ", Height:" << height << endl;
}
virtual string getClassName()
{
return "Rectangle";
}
private:
double width;
double height;
};
class Triangle :public Shape
{
public:
Triangle(double _side1, double _side2, double _side3)
{
side1 = _side1;
side2 = _side2;
side3 = _side3;
}
virtual double getArea()
{
double p = (side1 + side2 + side3) / 2;
return sqrt(p*(p - side1)*(p - side2)*(p - side3));
}
virtual double getPerimeter()
{
return side1 + side2 + side3;
}
virtual void show()
{
cout << "Side:" << side1 << ", " << side2 << ", " << side3 << endl;
}
virtual string getClassName()
{
return "Triangle";
}
private:
double side1;
double side2;
double side3;
};
int main()
{
double radius, width, height, side1, side2, side3;
cin >> radius;
Circle circle(radius);
cin >> width >> height;
Rectangle rectangle(width, height);
cin >> side1 >> side2 >> side3;
Triangle triangle(side1, side2, side3);
cout << circle.getClassName() << ":" << endl;
circle.show();
cout << "Area:" << circle.getArea();
cout << ", Perimeter:" << circle.getPerimeter() << endl;
cout << rectangle.getClassName() << ":" << endl;
rectangle.show();
cout << "Area:" << rectangle.getArea();
cout << ", Perimeter:" << rectangle.getPerimeter() << endl;
cout << triangle.getClassName() << ":" << endl;
triangle.show();
cout << "Area:" << triangle.getArea();
cout << ", Perimeter:" << triangle.getPerimeter() << endl;
return 0;
}
第十题
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
Employee(string _name)
{
name = _name;
}
string getName()
{
return name;
}
void setName(string _name)
{
name = _name;
}
virtual void show() = 0;
virtual double earnings() = 0;
private:
string name;
};
class Boss :public Employee
{
public:
Boss(string _name, double _weeklySalary)
:Employee(_name)
{
weeklySalary = _weeklySalary;
}
double getWeeklySalary()
{
return weeklySalary;
}
void setWeeklySalary(double _weeklySalary)
{
weeklySalary = _weeklySalary;
}
virtual void show()
{
cout << "Boss: " << getName() << endl;
}
virtual double earnings()
{
return weeklySalary;
}
private:
double weeklySalary;
};
class CommissionWorker :public Employee
{
public:
CommissionWorker(string _name, double _salary, double _commission, int _quantity)
:Employee(_name)
{
salary = _salary;
commission = _commission;
quantity = _quantity;
}
double getSalary()
{
return salary;
}
void setSalary(double _salary)
{
salary = _salary;
}
double getCommission()
{
return commission;
}
void setCommission(double _commission)
{
commission = _commission;
}
int getQuantity()
{
return quantity;
}
void setQuantity(int _quantity)
{
quantity = _quantity;
}
virtual void show()
{
cout << "Commission Worker: " << getName() << endl;
}
virtual double earnings()
{
return salary + commission*quantity;
}
private:
double salary;
double commission;
int quantity;
};
class PieceWorker :public Employee
{
public:
PieceWorker(string _name, double _wagePerPiece, int _quantity)
:Employee(_name)
{
wagePerPiece = _wagePerPiece;
quantity = _quantity;
}
double getWagePerPiece()
{
return wagePerPiece;
}
void setWagePerPiece(double _wagePerPiece)
{
wagePerPiece = _wagePerPiece;
}
int getQuantity()
{
return quantity;
}
void setQuantity(int _quantity)
{
quantity = _quantity;
}
virtual void show()
{
cout << "Piece Worker: " << getName() << endl;
}
virtual double earnings()
{
return wagePerPiece*quantity;
}
private:
double wagePerPiece;
int quantity;
};
class HourlyWorker :public Employee
{
public:
HourlyWorker(string _name, double _wage, double _hours)
:Employee(_name)
{
wage = _wage;
hours = _hours;
}
double getWage()
{
return wage;
}
void setWage(double _wage)
{
wage = _wage;
}
double getHours()
{
return hours;
}
void setHours(double _hours)
{
hours = _hours;
}
virtual void show()
{
cout << "Hourly Worker: " << getName() << endl;
}
virtual double earnings()
{
return wage*hours;
}
private:
double wage;
double hours;
};
int main()
{
string name;
double weeklySalary, salary, commission, quantity, wagePerPiece, wage, hours;
cin >> name >> weeklySalary;
Boss b(name, weeklySalary);
cin >> name >> salary >> commission >> quantity;
CommissionWorker c(name, salary, commission, quantity);
cin >> name >> wagePerPiece >> quantity;
PieceWorker p(name, wagePerPiece, quantity);
cin >> name >> wage >> hours;
HourlyWorker h(name, wage, hours);
Employee *ref;
ref = &b;
ref->show();
cout << "Earned: $" << ref->earnings() << endl;
ref = &c;
ref->show();
cout << "Earned: $" << ref->earnings() << endl;
ref = &p;
ref->show();
cout << "Earned: $" << ref->earnings() << endl;
ref = &h;
ref->show();
cout << "Earned: $" << ref->earnings() << endl;
return 0;
}