38 显示学校信息
#include <iostream> using namespace std; class person { private: char name[256]; int id; public: person() { name[0]='\0'; id=0; } virtual void put() { cout<<"Name:"<<name<<endl; cout<<"Number:"<<id<<endl; } virtual void get() { cin>>name>>id; } }; class student : person { private: char course[256]; double grade; public: student() { course[0]='\0'; grade=0; } virtual void put() { cout<<"Student information:"<<endl; person::put(); cout<<"Course:"<<course<<endl; cout<<"Grade:"<<grade<<endl; } virtual void get() { person::get(); cin>>course>>grade; } }; class teacher : person { private: char pro[256]; double paper; public: teacher() { pro[0]='\0'; paper=0; } virtual void put() { cout<<"Teacher information:"<<endl; person::put(); cout<<"pro:"<<pro<<endl; cout<<"paper:"<<paper<<endl; } virtual void get() { person::get(); cin>>pro>>paper; } }; int main() { person p; student p1; teacher p2; p1.get(); p2.get(); p1.put(); cout<<endl; p2.put(); system("pause"); return 0; }
39 图书系统
#include<iostream> #include<string> using namespace std; class person { private: char name[256]; int age; public: void setPerson(char* __name,int __age) { strcpy(name,__name); age=__age; } char* getName() { return name; } int getAge() { return age; } }; class book { private: int id; char name[256]; char press[256]; double price; person author; char print[256]; public: book(int __id,char* __name,char* __press,double __price,person __author,char* __print) { id=__id; strcpy(name,__name); strcpy(press,__press); price=__price; author=__author; strcpy(print,__print); } void showCard() { cout<<"Num:"<<id<<endl; cout<<"BookName:"<<name<<endl; cout<<"BookConcern:"<<press<<endl; cout<<"Price:"<<price<<endl; cout<<"AuthorName:"<<author.getName()<<endl; cout<<"AuthorAge:"<<author.getAge()<<endl; cout<<"PrintTime:"<<print<<endl; } }; int main() { char sysname[256]; cin>>sysname; cout<<"SysName:"<<sysname<<endl; char name[256],press[256],auname[256],Print[256]="\0",d[256],m[256],y[256]; int age,id; double price; cin>>id>>name>>press>>price>>auname>>age; cin>>y>>m>>d; strcat(Print,y); strcat(Print,"-"); strcat(Print,m); strcat(Print,"-"); strcat(Print,d); person author; author.setPerson(auname,age); book card(id,name,press,price,author,Print); card.showCard(); system("pause"); return 0; }
40 求图形面积
注:这道题费了点劲,因为虚函数定义的时候没写函数体,结果出了一堆莫名其妙的错误。
#include <iostream> #include <cmath> #define PI 3.14 using namespace std; class shape { protected: double x,y; public: shape(double __x=0,double __y=0) { x=__x; y=__y; } virtual double getSquare() { return 0; }; virtual char* getType() { return "shape"; return 0; }; friend ostream & operator<<(ostream & output,shape * obj); }; class triangle : public shape { public: triangle(double __x=0,double __y=0):shape(__x,__y) { } double getSquare() { return x*y/2; } char* getType(){ return "triangle"; } }; class rectangle : public shape { public: rectangle(double __x=0,double __y=0):shape(__x,__y){ } virtual double getSquare() { return x*y; } char* getType(){ return "rectangle"; } }; class circle : public shape { public: circle(double __x=0,double __y=0):shape(__x,__y){ } virtual double getSquare() { return pow(x,2)*PI; } char* getType(){ return "cirole"; } }; ostream & operator<<(ostream & output,shape * obj) { output<<obj->getType()<<"Area="; output<<obj->getSquare()<<endl; return output; } int main() { double x,y; cin>>x>>y; shape * s1=new triangle(x,y); shape * s2=new rectangle(x,y); shape * s3=new circle(x,y); cout<<s1<<s2<<s3; system("pause"); return 0; }
Leave a Reply
You must be logged in to post a comment.