1.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生出来的

要求输出一个圆桌的高度,面积与颜色等。

#include<iostream>
#include<string>
using namespace std;
#define PI 3.14;
class circle
{
public:
circle()
{
//默认构造函数
}
void setR(double r)
{
m_r = r;
}
double getR()
{
return m_r;
}
double getArea()
{
double area;
area = m_r *m_r * PI;
return area;
}
protected:
double m_r;//圆的半径
}; class table
{
public:
table()
{
//默认构造函数
}
void setHigh(double high)
{
m_high = high;
}
double getHigh()
{
return m_high;
}
protected:
double m_high;//桌子的高度
}; class roundtable : public circle, public table
{
public:
roundtable()
{
//构造函数 }
void setC(string color)
{
m_color = color;
}
string getC()
{
return m_color;
}
private:
string m_color;//圆桌的颜色
};
int main()
{
roundtable r1;
r1.setR(2.0);
r1.setHigh(1.3);
r1.setC("紫色");
cout << "圆桌的高为:" << r1.getHigh() << "米" << endl;
cout << "圆桌的面积为:" << r1.getArea() << "平方米" << endl;
cout << "圆桌的颜色为:" << r1.getC() << endl;
system("pause");
return 0;
}

2.定义描述矩形的类~~~~(题目略)

#include<iostream>
#include<string>
using namespace std;
class Rectangle
{
public:
Rectangle() {}
int CalArea()
{
int area;
area = m_Length * m_Width;
return area;
}
void setLength(int l)
{
m_Length = l;
}
int getLenght()
{
return m_Length;
}
void setWidth(int w)
{
m_Width = w;
}
int getWidth()
{
return m_Width;
}
protected:
int m_Length;
int m_Width;
}; class Cuboid :public Rectangle
{
public:
Cuboid(int l, int w, int h)
{
m_Length = l;
m_Width = w;
m_Chigh = h;
}
int CalVol()
{
m_Volume = m_Length * m_Chigh * m_Width;
return m_Volume;
}
void Show()
{
cout << "长方体的长为:" << m_Length << endl;
cout << "长方体的宽为:" << m_Width << endl;
cout << "长方体的高为:" << m_Chigh << endl;
cout << "长方体的体积为:" << CalVol() << endl;
}
private:
int m_Chigh;
int m_Volume;
}; int main()
{
Cuboid c1(10,20,30);
c1.Show();
system("pause");
return 0;
}

运行截图

3.定义一个车基类(题目略)

#include<iostream>
#include<string>
using namespace std;
class Vehicle
{
public:
Vehicle(int ms, int mw)
{
m_MaxSpeed = ms;
m_Weight = mw;
}
int getMaxSpeed()
{
return m_MaxSpeed;
}
int getWeight()
{
return m_Weight;
}
void Run()
{
cout << "车的最大速度为:" << m_MaxSpeed << endl;
cout << "车的重量为:" << m_Weight << endl;
}
void Stop()
{
cout << "车辆停下" << endl;
}
~Vehicle(){}
protected:
int m_MaxSpeed;
int m_Weight;
};
//虚基类的virtual可以写在public前面,如
//virtual public Vehicle
//也可以写在public后面
class bicycle :public virtual Vehicle
{
public:
bicycle(int ms, int mw, int h) :Vehicle(ms, mw)
{
m_height = h;
}
int getHeight()
{
return m_height;
}
void Run()
{
cout << "自行车的高为:" << m_height << endl;
}
void Stop()
{
cout << "自行车已停下" << endl;
}
~bicycle(){}
private:
int m_height;
}; class Motorcar :public virtual Vehicle
{
public:
Motorcar(int ms, int mw, int sn) :Vehicle(ms, mw)
{
m_SetNum = sn;
}
int getSetNum()
{
return m_SetNum;
}
void Run()
{
cout << "汽车的座位数为:" << m_SetNum << endl;
}
void Stop()
{
cout << "汽车已停下" << endl;
}
~Motorcar() {}
private:
int m_SetNum;
}; class motorcycle :public bicycle, public Motorcar
{
public:
motorcycle(int ms, int mw, int h, int sn) :Vehicle(ms, mw), bicycle(ms, mw, h), Motorcar(ms, mw, sn)
{ }
void Run()
{
cout << "这是motorcycle" << endl;
cout << "速度为:" << getMaxSpeed() << endl;
cout << "高度为:" << getHeight() << endl;
cout << "重量为为:" << getWeight() << endl;
cout << "座位数为:" << getSetNum() << endl;
}
void Stop()
{
cout << "motorcycle stopped" << endl;
}
~motorcycle(){}
}; int main()
{
Vehicle v1(10, 20);
v1.Run();
v1.Stop();
}

学习记录--C++继承与派生编程题的更多相关文章

  1. 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

    描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...

  2. C++学习之路—继承与派生(一):基本概念与基类成员的访问属性

    (本文根据<c++程序设计>(谭浩强)总结而成,整理者:华科小涛@http://www.cnblogs.com/hust-ghtao,转载请注明) 1   基本思想与概念 在传统的程序设计 ...

  3. buu学习记录(下)(做题是不可能做题的)

    目录: easy_calc 禁止套娃 EasyWeb Babysqli Easyweb easy_serialize_php 我有一个数据库 SSRFme 枯燥的抽奖 EasyPHP 题目解析: ea ...

  4. 【C++学习笔记】继承与派生基础概念

    面向对象的程序设计主要有四个特点:抽象.封装.继承和多态.其中继承是我认为最最重要的一个特性,可以说继承是面向对象的精华所在. 举一个继承的浅显易懂的例子:假如我们已经有了一个“马”的类,其中成员变量 ...

  5. C++学习之路—继承与派生(四)拓展与总结

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1    拓展部分 本节主要由两部分内容组成,分 ...

  6. C++学习之路—继承与派生(三):多重继承与虚基类

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多重继承是指一个派生类有两个或多个基类.例如,有 ...

  7. C++学习之路—继承与派生(二):派生类的构造函数与析构函数

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 由于基类的构造函数和析构函数是不能被继承的,所以 ...

  8. java学习记录笔记--继承,super,Object类

    继承: Java中的继承是单继承的. 1.子类拥有父类的全部属性和方法. 可是属性和方法的修饰符不能使private. 2.能够复用父类的代码. 方法的重写须要满足的条件: a.返回值类型 b.方法名 ...

  9. python核心编程学习记录之函数与函数式编程

    @func function 意思是func(function) @func(a) function 意思是func(a)这是个函数对象,在去调用function函数 如果要传额外的值,只传值用*tu ...

  10. POJ C++程序设计 编程题#1 编程作业—继承与派生

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 写一个MyStr ...

随机推荐

  1. oracle查看归档信息

    1.查看归档信息 show parameter recovery_file_dest #查看归档目录 show parameter recovery_file_dest_size #查看归档可以存储的 ...

  2. 关于zookeeper集群的理解

    一.zookeeper集群节点容错性 1.在集群模式下,建议至少部署3个zk进程,或者部署奇数个zk进程.如果只部署2个zk进程,当其中一个zk进程挂掉后,剩下的一个进程并不能构成一个quorum的大 ...

  3. 【STM32】NVIC嵌套中断向量控制器与外部中断

    两种优先级 抢占优先级PreemptPriority:中断服务函数正在执行时,抢占优先级高的可以打断抢占优先级低的,实现中断的嵌套,相当于51的"高优先级" 响应优先级(子优先级) ...

  4. Git配置新学

    Git中的AutoCRLF与SafeCRLF换行符问题 https://zhuanlan.zhihu.com/p/380574688 https://xiaozhuanlan.com/topic/40 ...

  5. HTML初体验之各种标签练习

    HTML初体验之各种标签练习 首先是<!DOCTYPE>标签 放在网页顶部的doctype声明是让浏览器进入正确呈现模式的关键.浏览器自动切换到恰当的呈现模式,以便正确显示由doctype ...

  6. snorkel SentenceNgramViewer 标记时出现 Integrity Error

    Snorkel使用SQLAlchemy作为数据管理工具,在本地会生成一个.db文件,现有操作与.db文件中已有的记录重复,违反了数据库主键不能重复的性质 解决方式: You can load a la ...

  7. leetcode-560-和为 K 的子数组

    给你一个整数数组 nums 和一个整数 k ,请你统计并返回 该数组中和为 k 的连续子数组的个数 . 示例 1: 输入:nums = [1,1,1], k = 2 输出:2 示例 2: 输入:num ...

  8. oracle函数及相关问题

    show user 查看当前用户select * from tab: 查看当前数据库中的表desc 表名 : 查看表结构 between and 包含开头结尾 函数: months_between(a ...

  9. 5、MySQL中的锁

    1.6. MySQL中的锁 InnoDB中锁非常多,总的来说,可以如下分类: 这些锁都是做什么的?具体含义是什么?我们现在来一一学习. 1.6.1.解决并发事务问题 我们已经知道事务并发执行时可能带来 ...

  10. js 导出json文件

    var data={}//处理得到的json字符串 var filename = 'json.json'//json文件名 if(typeof data === 'object'){ ddd = JS ...