实验要求
- 用标准C++完成程序
- 按照传统C语言风格进行命名和排版
- 在程序必要的地方进行注释
推荐实验环境
- VC 2003 ( 集成 Microsoft C++ Compiler 7.1)
- Dev-C++ ( 集成 MinGW,小巧,免费)
- Emacs + GCC ( Linux平台下开发工具 )
实验内容
- 使用递归的方法编写Fibonacci数列(1 1 2 3 5 8 ……)程序。输入n,输出数列中第n个数的值
用结构体、函数实现复数操作(复数的加减乘除),使如下的主程序可以正确运行
1 int main() { 2 struct complex a, b, sum, diff, product; 3 scanf("%f%f%f%f", &a.real, &a.imag, &b.real, &b.imag); 4 sum = add(a, b); 5 diff = substract(a, b); 6 product = multiply(a, b); 7 printf("sum:%f %f\n", sum.real, sum.imag); 8 printf("diff:%f %f\n", diff.real, diff.imag); 9 printf("product:%f %f\n", product.real, product.imag); 10 return 0; 11 }
使用标准C++输入输出实现在屏幕上用以下格式输出乘法表,要求排列整齐(不考虑屏幕宽度不够时造成的折行)。输入:乘法表的行数(1<=n<=99)输出:如下乘法表,总共n行
1 2 4 3 6 9 4 8 12 16 5 10 15 20 25 6 12 18 24 30 36 ……
- Monty Hall游戏:该游戏来自电视节目Let's Make a Deal. 游戏中设有三扇门,其中一扇门后面是一辆汽车,另外两扇门后面各有一头山羊。玩家首先选择其中一扇门,然后节目主持人将另外两扇门中藏有山的那扇门打开,并给游戏参与者一个改选的机会。此时玩家可以维持原先的选择不变,也可以直接选择另一扇门,或者随机决定如何选择。最后玩家如果选中了那个藏有汽车的门,则获得胜利。请你写一个程序,计算游戏中玩家如何选择概率较高,不同的选择概率各是多少?
- 将前面实验中的复数程序用C++类重新实现
做一个存放整数的Stack栈类,包含如下成员函数:构造函数Stack(n):n指定栈中可以存放元素的最大个数;push(i):把元素i添加到栈尾部;pop():把尾部元素从栈中删除;top():取栈尾部的元素的值;size():取栈中间存放的元素个数;~Stack():析构函数(如果有必要的话);Stack(Stack & s ):拷贝构造函数(如果有必要)。使如下程序可以正常运行:
改写前面的Stack类,使其可以统计Stack类对象的个数,GetStackNumber()函数返回对象的个数。
- 改写前面的Stack类,使Stack类在整个程序里面只能够存在一个对象。
- 用链表实现前面的stack类,实现相同的功能。
写Date类,实现如下功能:Date(year, month, day):构造函数,构造代表year年month月day日的Date对象;DaysFrom19900101():从1990年1月1日以来的天数,日期在1990年1月1日以后,返回正数,以前的返回负数;Add(days):计算出days天之后的新日期。days为负为之前;Delta(Date another):计算两天之间的日期差;Output():输出日期值,格式为“二〇〇五年十一月十三日”;WeekDay():返回这个日期是星期几,0表示星期天,1表示星期一,6表示星期六;静态成员函数today()返回今天的日期;所有的成员函数可以定义成const都定义成const。
实现Person, Student, GradStudent, Teacher类。类之间有正确的继承关系;每个类有对应的属性;每个类有构造函数,对属性初始化;每个类都没有默认构造函数;每个类都有一个print成员函数,输出属性的值。
用虚函数实现前面实验中的print函数
实现计算各种图形面积的程序。包括:Shape类:抽象基类,定义GetArea()和GetPerimeter()纯虚函数。Rectangle类:Shape的派生类,高、宽;Circle类:Shape的派生类,半径;每个类定义的构造函数,对成员进行初始化;在基类中定义虚函数GetArea, GetPerimeter,在派生类中override;每个类一个文件。
复数程序用运算符重载重新实现,使以下main函数可以运行
实现自己的string类,类的声明如下:
1 class string{ 2 char *str; int size; 3 public: 4 string(); 5 string(const char *s); 6 string(const string &s); 7 string& operator=(const char *); 8 string& operator=(const string&); 9 ~string(); 10 char &operator[](int i); 11 char operator[](int i) const; 12 bool operator==(const string &) const; 13 string operator+(const string &) const; 14 int length() const; 15 operator char*(); 16 friend ostream&operator<<(ostream&, const string&); 17 friend istream&operator>>(istream&, string &); 18 };
使用多继承实现,使以下程序可以运行:
1 int main() { 2 Person *p[6]; 3 p[0] = new Person(...); 4 p[1] = new Student(...); 5 p[2] = new Teacher( ... ); 6 Assistant *a = new Assistant(...); 7 Student *s = a; 8 Teacher *t = a; 9 p[3] = s; 10 p[4] = t; 11 p[5] = a; 12 s->print(); 13 t->print(); 14 for( int i = 0; i < 6; i++) 15 p[i]->print(); 16 }
实验参考答案
1 #include <stdio.h> 2 3 struct complex { 4 double imag; 5 double real; 6 }; 7 8 struct complex add(struct complex a, struct complex b) 9 { 10 struct complex c; 11 c.imag = a.imag + b.imag; 12 c.real = a.real + b.real; 13 return c; 14 } 15 16 struct complex substract(struct complex a, struct complex b) 17 { 18 struct complex c; 19 c.imag = a.imag - b.imag; 20 c.real = a.real - b.real; 21 return c; 22 } 23 24 struct complex multiply(struct complex a, struct complex b) 25 { 26 struct complex c; 27 c.imag = a.imag * b.real + a.real * b.imag; 28 c.real = a.real * b.real - a.imag * b.imag; 29 return c; 30 }
1 #include <iostream> 2 #include <stdlib.h> 3 #include <time.h> 4 5 using namespace std; 6 int main() { 7 int count; 8 cin >> count; 9 srand(time(NULL)); 10 int nochange = 0; 11 int change = 0; 12 int random = 0; 13 for(int i = 0; i < count; i++) { 14 int car = rand() % 3; 15 int choice = rand() % 3; 16 if(car == choice) 17 nochange ++; 18 else 19 change ++; 20 if(rand()%2) 21 random++; 22 } 23 cout << (double)nochange / count << endl; 24 cout << (double)change / count << endl; 25 cout << (double)random / count << endl; 26 }
1 #include <iostream> 2 using namespace std; 3 4 class complex { 5 public: 6 complex(double r = 0.0, double i = 0.0) 7 : real(r), imag(i) { 8 } 9 complex add(complex a) 10 { 11 complex c; 12 c.real = real + a.real; 13 c.imag = imag + a.imag; 14 return c; 15 } 16 void substract(complex a) { 17 imag -= a.imag; 18 real -= a.real; 19 } 20 void multiply(complex a) { 21 double r = real * a.real - imag * a.imag; 22 double i = real * a.imag + imag * a.real; 23 real = r; imag = i; 24 } 25 void print() const{ 26 cout << real << '\t' << imag; 27 } 28 private: 29 double imag; 30 double real; 31 32 }; 33 34 int main() { 35 double real, imag; 36 cin >> real >> imag; 37 complex a(real, imag); 38 cin >> real >> imag; 39 complex b(real, imag); 40 complex sum = a.add(b); 41 complex diff = a; 42 diff.substract(b); 43 complex product = a; 44 product.multiply(b); 45 sum.print(); 46 diff.print(); 47 product.print(); 48 return 0; 49 }
1 #include <iostream> 2 #include <stdexcept> 3 using namespace std; 4 5 class stack { 6 public: 7 stack(int capacity) 8 : capacity_ (capacity), size_(0), 9 data_ (new int[capacity]) { 10 } 11 void push(int i) { 12 if(size_ < capacity_) 13 data_[size_++] = i; 14 } 15 void pop() { 16 if(size_ > 0) 17 size_ --; 18 } 19 int top() const { 20 if(size_ > 0) 21 return data_[size_-1]; 22 else 23 throw std::out_of_range(""); 24 } 25 int size() const { 26 return size_; 27 } 28 ~stack() { 29 delete[] data_; 30 } 31 stack(stack& s) 32 : size_(s.size_), capacity_(s.capacity_), 33 data_(new int[s.capacity_]) { 34 for(int i =0; i < size_; i++) 35 data_[i] = s.data_[i]; 36 } 37 private: 38 int *data_; 39 int size_; 40 int capacity_; 41 };
1 #include <iostream> 2 #include <stdexcept> 3 using namespace std; 4 5 class stack { 6 public: 7 stack(int capacity) 8 : capacity_ (capacity), size_(0), data_(new int[capacity]) { 9 stack_count_++; 10 } 11 void push(int i) { 12 if(size_ < capacity_) 13 data_[size_++] = i; 14 } 15 void pop() { 16 if(size_ > 0) 17 size_ --; 18 } 19 int top() const { 20 if(size_ > 0) 21 return data_[size_-1]; 22 else 23 throw std::out_of_range(""); 24 } 25 int size() const { 26 return size_; 27 } 28 ~stack() { 29 stack_count_--; 30 delete[] data_; 31 } 32 stack(stack& s) 33 : size_(s.size_), capacity_(s.capacity_), data_(new int[s.capacity_]) { 34 stack_count_++; 35 for(int i =0; i < size_; i++) 36 data_[i] = s.data_[i]; 37 } 38 static int GetStackNumber() { 39 return stack_count_; 40 } 41 private: 42 static int stack_count_; 43 int *data_; 44 int size_; 45 int capacity_; 46 }; 47 48 int stack::stack_count_; 49 50 int main() { 51 cout << stack::GetStackNumber()<< endl; 52 stack s1(5); 53 cout << s1.GetStackNumber()<< endl; 54 s1.push(1); 55 s1.push(2); 56 s1.push(3); 57 stack s2(s1); 58 cout << s2.GetStackNumber()<< endl; 59 while(s1.size() > 0) { 60 cout << s1.top() << endl; 61 s1.pop(); 62 } 63 while(s2.size() > 0) { 64 cout << s2.top() << endl; 65 s2.pop(); 66 } 67 }
1 #include <iostream> 2 #include <stdexcept> 3 using namespace std; 4 5 class stack { 6 public: 7 void push(int i) { 8 if(size_ < capacity_) 9 data_[size_++] = i; 10 } 11 void pop() { 12 if(size_ > 0) 13 size_ --; 14 } 15 int top() const { 16 if(size_ > 0) 17 return data_[size_-1]; 18 else 19 throw std::out_of_range(""); 20 } 21 int size() const { 22 return size_; 23 } 24 ~stack() { 25 delete[] data_; 26 } 27 static stack &create(int capacity) { 28 static stack s(capacity); 29 return s; 30 } 31 private: 32 stack(stack& s); 33 34 stack(int capacity) 35 : capacity_ (capacity), size_(0), data_(new int[capacity]) { 36 } 37 38 int *data_; 39 int size_; 40 int capacity_; 41 }; 42 43 int main() { 44 stack &s1 = stack::create(5); 45 s1.push(1); 46 s1.push(2); 47 s1.push(3); 48 stack &s2 = stack::create(6); 49 while(s1.size() > 0) { 50 cout << s1.top() << endl; 51 s1.pop(); 52 } 53 while(s2.size() > 0) { 54 cout << s2.top() << endl; 55 s2.pop(); 56 } 57 }
1 #include <iostream> 2 #include <stdexcept> 3 #include <stdlib.h> 4 using namespace std; 5 struct node{ 6 int data; 7 node *next; 8 }; 9 class stack{ 10 private: 11 node *head; 12 public: 13 stack(){ 14 head=NULL; 15 } 16 void push(int i){ 17 node *p=new node; 18 p->next=head; 19 p->data=i; 20 head=p; 21 } 22 void pop(){ 23 node *p=head; 24 if(p!=NULL){ 25 head=head->next; 26 delete p; 27 } 28 } 29 int size(){ 30 int count=0; 31 for(node *p=head;p!=NULL;p=p->next) 32 count++; 33 return count; 34 } 35 int top(){ 36 return head->data; 37 } 38 stack(stack &s){ 39 head=NULL; 40 node *tail=NULL; 41 for(node *q=s.head;q!=NULL;q=q->next){ 42 node *p=new node; 43 p->data=q->data; 44 p->next=NULL; 45 if(tail==NULL) 46 tail=head=p; 47 else{ 48 tail->next=p; 49 tail=p; 50 } 51 } 52 } 53 ~stack(){ 54 while(head!=NULL) 55 pop(); 56 } 57 };
Date.h:
1 #ifndef DATE_H 2 #define DATE_H 3 4 class Date { 5 public: 6 bool SetDate( int, int, int ); // set the date 7 8 int DaysFrom19900101() const; 9 Date Add(int delta) const; 10 int Delta(const Date &anotherDate) const; 11 void Output() const; 12 13 void increment(); 14 void decrement(); 15 bool leap_year() const; 16 bool is_equal(const Date &anotherDate) const; 17 int weekday() const; 18 static Date today(); 19 private: 20 int month; 21 int day; 22 int year; 23 bool is_valid() const; 24 }; 25 26 #endif 27
Date.cc:
1 #include <iostream> 2 using namespace std; 3 4 #include "date.h" 5 6 int nonleap_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 7 int leap_days[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 8 char *chinese[] = {"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; 9 10 bool Date::leap_year() const 11 { 12 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 13 } 14 15 bool Date::is_valid() const 16 { 17 return year > 0 && month >=1 && month <=12 && day >=0 && 18 ( leap_year() ? day <= leap_days[month-1] : day <= nonleap_days[month-1]); 19 } 20 21 bool Date::is_equal(const Date &a) const 22 { 23 return year == a.year && month == a.month && day == a.day; 24 } 25 26 void Date::increment() 27 { 28 day++; 29 if(!is_valid()) { 30 day = 1; month++; 31 if(!is_valid()) { 32 month = 1; 33 year ++; 34 } 35 } 36 } 37 38 void Date::decrement() 39 { 40 day--; 41 if(day == 0) { 42 month--; 43 if(month == 0) { 44 year --; 45 month = 12; 46 } 47 day = leap_year() ? leap_days[month-1] : nonleap_days[month-1]; 48 } 49 } 50 51 bool Date::SetDate(int y, int m, int d) 52 { 53 year = y; 54 month = m; 55 day = d; 56 return is_valid(); 57 } 58 59 int Date::DaysFrom19900101() const 60 { 61 Date d; 62 d.SetDate(1900, 1, 1); 63 return Delta(d); 64 } 65 66 Date Date::Add(int delta) const 67 { 68 Date d(*this); 69 for(; delta > 0; delta--) 70 d.increment(); 71 for(; delta < 0; delta++) 72 d.decrement(); 73 return d; 74 } 75 76 int Date::Delta(const Date &anotherDate) const 77 { 78 int diff = 0; 79 Date a (anotherDate); 80 if( year > a.year || year == a.year && month > a.month || 81 year == a.year && month == a.month && day > a.day) 82 for(; !is_equal(a); diff++) 83 a.increment(); 84 else 85 for(; !is_equal(a); diff--) 86 a.decrement(); 87 return diff; 88 } 89 90 void Date::Output() const 91 { 92 int dy = 1; 93 for(int y = year; y >= 10; y /= 10) { 94 dy *= 10; 95 } 96 97 for(int y = year; dy > 0; y%=dy, dy/=10) { 98 cout << chinese[ (y - y % dy)/dy]; 99 } 100 cout << "年"; 101 if(month >= 10) 102 cout << "十"; 103 if(month % 10 !=0) 104 cout << chinese[month % 10]; 105 cout << "月"; 106 if(day >= 20) 107 cout << chinese[day / 10]; 108 if(day >= 10) 109 cout << "十"; 110 if(day % 10 != 0) 111 cout << chinese[day % 10]; 112 cout <<"日"; 113 } 114 115 int Date::weekday() const{ 116 Date d(2005, 12, 25); 117 int delta = Delta(d); 118 if(delta >= 0) 119 return delta % 7; 120 else 121 return ( 7 - (-delta % 7) ) % 7; 122 } 123 Date Date::today() { 124 time_t t = time(NULL); 125 tm *lt = localtime(&t); 126 Date d; 127 d.SetDate(lt->tm_year + 1900, lt->tm_mon+1, lt->tm_mday); 128 return d; 129 }
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 #define MALE true 6 #define FEMALE false 7 struct Date{ 8 int year, month, day; 9 Date(int y, int m, int d): year(y), month(m), day(d) {} 10 }; 11 12 class Person{ 13 public: 14 Person(string name, Date birth, bool gender) 15 : name_(name), birth_(birth), gender_(gender) { 16 } 17 virtual void print() const { 18 cout << name_; 19 cout << birth_.year << birth_.month << birth_.day; 20 cout << (gender_?"男":"女"); 21 } 22 protected: 23 string name_ ; 24 Date birth_; 25 bool gender_; 26 }; 27 28 class Student : public Person{ 29 public: 30 Student(string name, Date birth, bool gender, string id, string department) 31 : Person(name, birth, gender), id_(id), department_(department) 32 {} 33 void print() const { 34 Person::print(); 35 cout << id_ << department_; 36 } 37 protected: 38 string id_; 39 string department_; 40 }; 41 42 43 class Teacher : public Person{ 44 public: 45 Teacher(string name, Date birth, bool gender, string title, double salary) 46 : Person(name, birth, gender), title_(title), salary_(salary) 47 {} 48 void print() const { 49 Person::print(); 50 cout << title_ << salary_; 51 } 52 protected: 53 string title_; 54 double salary_; 55 }; 56 57 class GradStudent : public Student{ 58 public: 59 GradStudent(string name, Date birth, bool gender, string id, string department, Teacher *t) 60 : Student(name, birth, gender, id, department) , tutor_(t) 61 {} 62 void print() const { 63 Student::print(); 64 tutor_->print(); 65 } 66 protected: 67 Teacher *tutor_; 68 }; 69 70 int main() { 71 Person *p = new Person( "jack", Date(1980, 8, 8), MALE ); 72 Student *s = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); 73 Teacher *t = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); 74 GradStudent *g = new GradStudent("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", t); 75 p->print(); 76 cout << endl; 77 s->print(); 78 cout << endl; 79 t->print(); 80 cout << endl; 81 g->print(); 82 cout << endl; 83 delete g; 84 delete p; 85 delete s; 86 delete t; 87 }
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 #define MALE true 6 #define FEMALE false 7 struct Date{ 8 int year, month, day; 9 Date(int y, int m, int d): year(y), month(m), day(d) {} 10 }; 11 12 class Person{ 13 public: 14 Person(string name, Date birth, bool gender) 15 : name_(name), birth_(birth), gender_(gender) { 16 } 17 virtual void print() const { 18 cout << name_; 19 cout << birth_.year << birth_.month << birth_.day; 20 cout << (gender_?"男":"女"); 21 } 22 protected: 23 string name_ ; 24 Date birth_; 25 bool gender_; 26 }; 27 28 class Student : public Person{ 29 public: 30 Student(string name, Date birth, bool gender, string id, string department) 31 : Person(name, birth, gender), id_(id), department_(department) 32 {} 33 void print() const { 34 Person::print(); 35 cout << id_ << department_; 36 } 37 protected: 38 string id_; 39 string department_; 40 }; 41 42 43 class Teacher : public Person{ 44 public: 45 Teacher(string name, Date birth, bool gender, string title, double salary) 46 : Person(name, birth, gender), title_(title), salary_(salary) 47 {} 48 void print() const { 49 Person::print(); 50 cout << title_ << salary_; 51 } 52 protected: 53 string title_; 54 double salary_; 55 }; 56 57 class GradStudent : public Student{ 58 public: 59 GradStudent(string name, Date birth, bool gender, string id, string department, Teacher *t) 60 : Student(name, birth, gender, id, department) , tutor_(t) 61 {} 62 void print() const { 63 Student::print(); 64 tutor_->print(); 65 } 66 protected: 67 Teacher *tutor_; 68 }; 69 70 int main() { 71 Person *p[4]; 72 p[0] = new Person( "jack", Date(1980, 8, 8), MALE ); 73 p[1] = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); 74 p[2] = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); 75 p[3] = new GradStudent("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", (Teacher *)p[2]); 76 for(int i = 0; i < 4; i++) { 77 p[i] -> print(); 78 cout << endl; 79 } 80 }
shape.h:
circle.h:
1 #ifndef CIRCLE 2 #define CIRCLE 3 #define PI 3.14 4 class Circle : public Shape{ 5 public: 6 Circle(double radius_){ 7 radius=radius_; 8 } 9 virtual double GetArea(){ 10 return PI*radius*radius; 11 } 12 virtual double GetPerimeter(){ 13 return 2*PI*radius; 14 } 15 protected: 16 double radius; 17 }; 18 #endif 19
rectangle.h:
1 #ifndef RECTANGLE 2 #define RECTANGLE 3 class Rectangle : public Shape{ 4 public: 5 Rectangle(double height_,double width_){ 6 height=height_; 7 width=width_; 8 } 9 virtual double GetArea(){ 10 return height*width; 11 } 12 virtual double GetPerimeter(){ 13 return 2*(height+width); 14 } 15 protected: 16 double height,width; 17 }; 18 #endif 19
main.cc:
1 #include<stdlib.h> 2 #include<iostream> 3 #include"Shape.h" 4 #include"Rectangle.h" 5 #include"Circle.h" 6 using namespace std; 7 int main() { 8 Shape *s[2]; 9 s[0] = new Rectangle(4,3); 10 s[1] = new Circle(5); 11 for ( int i = 0; i < 2; i++){ 12 cout<<"Area:"<<s[i]->GetArea()<<endl; 13 cout<<"Perimeter:"<<s[i]->GetPerimeter()<<endl; 14 } 15 }
1 #include <iostream> 2 using namespace std; 3 class complex 4 { 5 public: 6 complex(double r = 0.0,double i = 0.0) : real_(r), imag_(i) {} 7 double &real(){ return real_; } 8 double &imag(){ return imag_; } 9 double real() const { return real_; } 10 double imag() const { return imag_; } 11 complex &operator+=(const complex &a) { 12 real_ += a.real_; 13 imag_ += a.imag_; 14 return *this; 15 } 16 complex &operator-=(const complex &a) { 17 real_ -= a.real_; 18 imag_ -= a.imag_; 19 return *this; 20 } 21 complex &operator*=(const complex &a) { 22 double r = real_ * a.real_ - imag_ * a.imag_; 23 double i = real_ * a.imag_ + imag_ * a.real_; 24 real_ = r; 25 imag_ = i; 26 return *this; 27 } 28 private: 29 double real_,imag_; 30 }; 31 32 bool operator==(const complex &a, const complex &b){ 33 return a.real() == b.real() && a.imag() == b.imag(); 34 } 35 36 ostream &operator<<(ostream &os, const complex &c) { 37 return os << "(" << c.real()<< showpos << c.imag() << "i)"; 38 } 39 istream &operator>>(istream &is, complex &c) { 40 return is >> c.real() >> c.imag(); 41 } 42 complex operator+(complex a, const complex &b){ 43 return a += b; 44 } 45 complex operator-(complex a, const complex &b) { 46 return a -= b; 47 } 48 complex operator*(complex a, const complex &b) { 49 return a *= b; 50 }
1 #include <iostream> 2 3 class string{ 4 char *str; int size; 5 public: 6 string() : size(1), str(new char[1]) { str[0] = '\0'; } 7 string(const char *s) { size = strlen(s)+1; str = new char[size]; strcpy(str, s); } 8 string(const string &s) { size = s.size; str = new char[size]; strcpy(str, s.str); } 9 string& operator=(const char *s) { delete[] str; size = strlen(s); str = new char[size]; strcpy(str, s); } 10 string& operator=(const string&s) { if(this!=&s) { delete[] str; size = s.size; str= new char[size]; strcpy(str, s.str); }} 11 ~string() { delete [] str;} 12 char &operator[](int i) { return str[i];} 13 char operator[](int i) const { return str[i]; } 14 bool operator==(const string &s) const { return strcmp(str, s.str) == 0; } 15 string operator+(const string &s) const { 16 char *t = new char[size+s.size-1]; 17 strcpy(t, str); 18 strcat(t, s.str); 19 string r(t); 20 delete[] t; 21 return r; 22 } 23 int length() const { return size - 1; } 24 friend std::ostream&operator<<(std::ostream&os, const string&s) { return os << s.str; } 25 friend std::istream&operator>>(std::istream&is, string &s) { 26 int capacity = 10; 27 char *t = new char[capacity]; 28 int i = 0; 29 int c; 30 while( !isspace(c = is.get())) { 31 t[i++] = c; 32 if(i == capacity) { 33 char * t2 = new char [capacity * 2]; 34 for(int j = 0; j < capacity; j++) 35 t2[j] = t[j]; 36 capacity *= 2; 37 delete[] t; 38 t = t2; 39 } 40 } 41 t[i] = '\0'; 42 s = t; 43 delete[] t; 44 return is; 45 } 46 };
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 #define MALE true 6 #define FEMALE false 7 struct Date{ 8 int year, month, day; 9 Date(int y, int m, int d): year(y), month(m), day(d) {} 10 }; 11 12 class Person{ 13 public: 14 Person(string name, Date birth, bool gender) 15 : name_(name), birth_(birth), gender_(gender) { 16 } 17 virtual void print() const { 18 cout << name_; 19 cout << birth_.year << birth_.month << birth_.day; 20 cout << (gender_?"男":"女"); 21 } 22 virtual ~Person(){} 23 protected: 24 string name_ ; 25 Date birth_; 26 bool gender_; 27 }; 28 29 class Student : virtual public Person{ 30 public: 31 Student(string name, Date birth, bool gender, string id, string department) 32 : Person(name, birth, gender), id_(id), department_(department) 33 {} 34 void print() const { 35 Person::print(); 36 cout << id_ << department_; 37 } 38 protected: 39 string id_; 40 string department_; 41 }; 42 43 44 class Teacher : virtual public Person{ 45 public: 46 Teacher(string name, Date birth, bool gender, string title, double salary) 47 : Person(name, birth, gender), title_(title), salary_(salary) 48 {} 49 void print() const { 50 Person::print(); 51 cout << title_ << salary_; 52 } 53 protected: 54 string title_; 55 double salary_; 56 }; 57 58 class Assistant : public Student, public Teacher{ 59 public: 60 Assistant(string name, Date birth, bool gender, string id, string department, string title, double salary) 61 : Student(name, birth, gender, id, department) , 62 Teacher(name, birth, gender, title, salary), Person(name, birth, gender) 63 {} 64 void print() const { 65 Student::print(); 66 Teacher::print(); 67 } 68 }; 69 70 int main() { 71 Person *p[6]; 72 p[0] = new Person( "jack", Date(1980, 8, 8), MALE ); 73 p[1] = new Student( "rose", Date(1982, 8, 7), FEMALE, "0203813", "computer" ); 74 p[2] = new Teacher( "czk", Date(1979, 8, 27), MALE, "professor", 1000); 75 Assistant *a = new Assistant("lisa", Date(1982, 8, 7), FEMALE, "0203812", "computer", "assistant", 500); 76 Student *s = a; 77 Teacher *t = a; 78 p[3] = s; 79 p[4] = t; 80 p[5] = a; 81 s->print(); 82 t->print(); 83 for( int i = 0; i < 6; i++) { 84 p[i]->print(); 85 cout << endl; 86 } 87 delete p[0]; 88 delete p[1]; 89 delete p[2]; 90 delete a; 91 }