C++学习3

本文详细介绍了C++语言中继承与派生的概念,并通过多个实例展示了如何定义point类、Rectangle类、circle类、Building类、Teach_Building类、Dorm_Building类、Table类、RoundTable类、Person类、Student类、Teacher类、Clock类、NewClock类、ClockWithDate类、String类扩展、工资管理系统等,涵盖面向对象编程的核心思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

课后习题编程题(C++语言程序设计 杨进才版)

第八章 继承与派生

P280 

//1.定义一个point类,派生出Rectangle类和circle类
#include <iostream>
#include <cmath>
using namespace std;
class point
{
    protected:
        int x,y;
    public:
        point(int x,int y)
        {
            this->x=x;
            this->y=y;
        }
        double distance(point a)
        {
            return sqrt((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y));
        }
        void showxy()
        {
            cout<<"("<<x<<","<<y<<")"<<endl;
        }
};
class ppoint
{
    protected:
        int x,y;
    public:
        ppoint(int x,int y)
        {
            this->x=x;
            this->y=y;
        }
        double distance(ppoint a)
        {
            return sqrt((a.x-x)*(a.x-x)+(a.y-y)*(a.y-y));
        }
        void showxy()
        {
            cout<<"("<<x<<","<<y<<")"<<endl;
        }
};
class Rectangle:protected point,protected ppoint
{
    private:
        double length;
        double wide;
    public:
        Rectangle(int x1,int y1,int x2,int y2):point(x1,y1),ppoint(x2,y2)
        {}
        double getlength()
        {
            double x1=point::x;
            double x2=ppoint::x;
            double y1=point::y;
            double y2=ppoint::y;
            double l1=(x1-x2)>0?(x1-x2):(x2-x1);
            double l2=(y1-y2)>0?(y1-y2):(y2-y1);
            return (l1>l2)?l1:l2;
        }
        double getwide()
        {
            double x1=point::x;
            double x2=ppoint::x;
            double y1=point::y;
            double y2=ppoint::y;
            double l1=(x1-x2)>0?(x1-x2):(x2-x1);
            double l2=(y1-y2)>0?(y1-y2):(y2-y1);
            return (l1<l2)?l1:l2;
        }
        double area()
        {
            return getlength()*getwide();
        }
        void show()
        {
            cout<<"the two point of Rectangle is : "<<endl;
            cout<<"   ";point::showxy();
            cout<<"   ";ppoint::showxy();
            cout<<endl;
            cout<<"the area of Rectangle : "<<area()<<endl;
        }
};
class circle:protected point
{
    private:
        double r;
    public:
        circle(double r,int x,int y):point(x,y)
        {
            this->r=r;
        }
        double area()
        {
            return 3.14*r*r;
        }
        void show()
        {
            cout<<"the area of the circle :"<<area()<<endl;
            cout<<"the centre of the circle :";
            point::showxy();
            cout<<endl;
        }
};
int main()
{
    Rectangle R(0,0,1,1);//输入矩形对角两点的坐标
    R.show();
    circle c(10,5,5);
    c.show();
}


//2.设计一个Building类,派生出Teach_Building和Dorm_Building
#include <iostream>
using namespace std;
class Building
{
    protected:
        int cengshu;
        int area;
        int classnum;
    public:
        Building(int cengshu=0,int area=0,int classnum=0)
        {
            this->cengshu=cengshu;
            this->area=area;
            this->classnum=classnum;
        }
        void show()
        {
            cout<<" cengshu: "<<cengshu<<endl;
            cout<<" area : "<<area<<endl;
            cout<<" house num: "<<classnum<<endl;
        }
};
class Teach_Building:protected Building
{
    protected:
        int no;
    public:
        Teach_Building(int no,int cengshu,int area,int classnum ):Building(cengshu,area,classnum)
        {
            this->no=no;
        }
        void show()
        {
            cout<<"The Teach_Building No "<<no<<endl;
            Building::show();
            cout<<endl;
        }
};
class Dorm_Building:protected Building
{
    protected:
        int no;
        int num_stu;
    public:
        Dorm_Building(int no,int cengshu,int area,int numroom,int num_stu):Building(cengshu,area,numroom)
        {
            this->no=no;
            this->num_stu=num_stu;
        }
        void show()
        {
            cout<<"The Dormitory No "<<no<<endl;
            cout<<"the number of student "<<num_stu<<endl;
            Building::show();
            cout<<endl;
        }
};
int main()
{
    Teach_Building T(9,11,1000,20);
    T.show();
    Dorm_Building D(16,3,1000,20,300);
    D.show();
    return 0;
}


//3.定义一个Table类和Circle类,共同派生出RoundTable
#include <iostream>
using namespace std;
class point
{
    protected:
        int x,y;
    public:
        point(int x,int y)
        {
            this->x=x;
            this->y=y;
        }
        void show()
        {
            cout<<"("<<x<<","<<y<<")"<<endl;
        }
};
class circle:protected point
{
    protected:
        double r;
    public:
        circle(double r,int x,int y):point(x,y)
        {
            this->r=r;
        }
        void show()
        {
            cout<<"the centre of circle :";
            point::show();
            cout<<"the radius of circle :"<<r<<endl;
        }
};
class Table
{
    protected:
        double heigth;
    public:
        Table(double h)
        {
            heigth=h;
        }
        void show()
        {
            cout<<"the table's heigth is : "<<heigth<<endl;
        }
};
class RoundTable:protected circle,protected Table
{
    public:
        RoundTable(double heigth,double r,int x,int y):circle(r,x,y),Table(heigth)
        {}
        void show()
        {
            cout<<"This is a RoundTable !"<<endl;
            circle::show();
            Table::show();
        }
};
int main()
{
    RoundTable RT(1,20,0,0);
    RT.show();
    return 0;
}


//4.定义一个人员类Person,派生出Student类和Teacher类
//共同派生出Stu_Tech类,Person类有姓名,性别,身份证,生日
//Student类有学习成绩,学号,Teacher类有职称
#include <iostream>
#include <cstring>
using namespace std;
class Person
{
    protected:
        char name[10];
        char sex[5];
        char identy[20];
        int birth;
    public:
        Person(char s1[],char s2[],char s3[],int num)
        {
            strcpy(this->name,s1);
            strcpy(this->sex,s2);
            strcpy(this->identy,s3);
            this->birth=num;
        }
        void show()
        {
            cout<<"Introduction !!"<<endl;
            cout<<"  Name "<<name<<endl;
            cout<<"  Sex "<<sex<<endl;
            cout<<"  ID "<<identy<<endl;
            cout<<"  Birthday "<<birth<<endl;
        }
};
class Student:virtual protected Person
{
    protected:
        int schoolnum;
        int grade;
    public:
        Student(int schoolnum,int grade,char s1[],char s2[],char s3[],int num):Person(s1,s2,s3,num)
        {
            this->schoolnum=schoolnum;
            this->grade=grade;
        }
        void show()
        {
            cout<<"I'm a student !"<<endl;
            //Person::show();
            cout<<"the school num : "<<schoolnum<<endl;
            cout<<"the grade is : "<<grade<<endl;
        }
};
class Teacher:virtual protected Person
{
    protected:
        char job[10];
    public:
        Teacher(char job[],char s1[],char s2[],char s3[],int num):Person(s1,s2,s3,num)
        {
            strcpy(this->job,job);
        }
        void show()
        {
            cout<<"I'm a teacher !"<<endl;
            Person::show();
            cout<<"the job : "<<job<<endl;
        }
};
class Stu_Tech:protected Student,protected Teacher
{
    public:
        Stu_Tech(char job[],int schoolnum,int grade,char s1[],char s2[],char s3[],int num):Student(schoolnum,grade,s1,s2,s3,num),Teacher(job,s1,s2,s3,num),Person(s1,s2,s3,num)
        {
        }
        void show()
        {
            cout<<"I'm a student and a teacher !"<<endl;
            Student::show();
            Teacher::show();
        }
};
int main()
{
    Teacher T("math","kaimei","M","555555199305225555",19930522);
    T.show();
    cout<<endl;
    Stu_Tech ST("chinese",2011123456,99,"pika","M","555555199305225555",19930522);
    ST.show();
    return 0;
}

//5.利用Clock定义一个带”AM“和”PM“的新时钟类NewClock
#include <iostream>
#include <ctime>
using namespace std;
class Clock
{
    protected:
        int h,m,s;
    public:
        Clock()
        {
            time_t t=time(0);
            tm *curtime=localtime(&t);
            h=curtime->tm_hour;
            m=curtime->tm_min;
            s=curtime->tm_sec;
        }
        void show()
        {
            cout<<h<<":"<<m<<":"<<s<<endl;
        }
};
class NewClock:protected Clock
{
    public:
        NewClock():Clock() {}
        void show()
        {
            if(h<12)
                cout<<"AM ";
            else
                cout<<"PM ";
            Clock::show();
        }
};
int main()
{
    NewClock c;
    c.show();
    return 0;
}


//6.利用Clock类和Date类定义一个带日期的ClockWithDate
// 跟前面一样的,不想写了,注意一下加一秒到下一天或者下一个月,还有瑞年问题

//7.扩充String类,增加编辑函数,替换字符,替换子串,删除字符,删除子串
//看着就晕了,不想写,估计不会考
 

//8.公司的工资管理(可能稍微有那么一点不好写吧)
// manager technician saleman salesmanager
//好吧,今天实在是不想写了,这个就是先建一个Person类
//然后派生出上面四种职位,Person类中包含基础信息,名字等
//protected派生,然后调用基础工资得到各自工资
  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值