C++程序示例:涉及到抽象类、继承…
2014-03-12 15:16
246 查看
C++复习题。
有助于理解:
.h与.cpp分离
基类、派生类、抽象类
多态、纯虚函数
对象指针、引用
派生类使用基类的构造函数
。。。
==================================================================================================
t3.h
#ifndef T3_H_INCLUDED
#define T3_H_INCLUDED
//此程序定义一个Shape类。矩形,圆形从Shape继承。矩形可以计算面积,改变大小,比较大小。改变大小,比较大小。
//继承、虚函数、重载
//protected成员可以被派生类直接访问,private成员不可以被派生类访问
//涉及析构
class Shape{
protected:
float length;
float width;
float radius;
public:
Shape();
Shape(float r);
Shape(float l,float w);
~Shape();
float getRadius();
float getWidth();
float getLength();
virtual void display()=0;
virtual void changeSize(float l,float w)=0;
virtual double getArea()=0;
virtual bool compareSize(Shape* s0)=0;
};
class Rectangular:public Shape{
public:
Rectangular():Shape(){}
Rectangular(float l,float w):Shape(l,w){}
void display();
void changeSize(float l,float w);
double getArea();
bool compareSize(Shape* s0);
};
class Circle:public Shape{
public:
Circle():Shape(){}
Circle(float r):Shape(r){}
void display();
void changeSize(float l,float w);
double getArea();
bool compareSize(Shape* s0);
};
#endif
==================================================================================================
t3.cpp
#include
#include "t3.h"
using namespace std;
Shape::Shape(){length=0;width=0;radius=0;}
Shape::Shape(float r){radius=r;}
Shape::Shape(float l,float w){length=l;width=w;}
Shape::~Shape(){
cout<<"I was dead."<<endl;
}
float Shape::getRadius(){
return radius;
}
float Shape::getLength(){
return length;
}
float Shape::getWidth(){
return width;
}
//Shape end
void Rectangular::display(){
cout<<"I'm a rectangular."<<endl;
}
void Rectangular::changeSize(float l,float w){
length=length+l;
width=width+w;
}
double Rectangular::getArea(){
return length*width;
}
bool Rectangular::compareSize(Shape* s0){
return this->getArea()>s0->getArea();
}
//Rectangular end
void Circle::display(){
cout<<"I'm a circle."<<endl;
}
void Circle::changeSize(float l,float w=0){
radius=radius+l;
}
double Circle::getArea(){
return 3.14*radius*radius;
}
bool Circle::compareSize(Shape* s0){
return this->getArea()>s0->getArea();
}
//Circle end
int main(){
Rectangular r1=Rectangular();
r1.display();
cout<<"My area
is:"<<r1.getArea()<<endl;
r1.changeSize(5,6);
r1.display();
cout<<"My area
is:"<<r1.getArea()<<endl;
Rectangular r2=Rectangular(3,4);
r2.display();
cout<<"My area
is:"<<r2.getArea()<<endl;
Circle c1=Circle();
c1.display();
cout<<"My area
is:"<<c1.getArea()<<endl;
c1.changeSize(5);
c1.display();
cout<<"My area
is:"<<c1.getArea()<<endl;
Circle c2=Circle(6);
c2.display();
cout<<"My area
is:"<<c2.getArea()<<endl;
Shape *p;
p=&c2;
cout<<r2.compareSize(p)<<endl;;
}
有助于理解:
.h与.cpp分离
基类、派生类、抽象类
多态、纯虚函数
对象指针、引用
派生类使用基类的构造函数
。。。
==================================================================================================
t3.h
#ifndef T3_H_INCLUDED
#define T3_H_INCLUDED
//此程序定义一个Shape类。矩形,圆形从Shape继承。矩形可以计算面积,改变大小,比较大小。改变大小,比较大小。
//继承、虚函数、重载
//protected成员可以被派生类直接访问,private成员不可以被派生类访问
//涉及析构
class Shape{
protected:
float length;
float width;
float radius;
public:
Shape();
Shape(float r);
Shape(float l,float w);
~Shape();
float getRadius();
float getWidth();
float getLength();
virtual void display()=0;
virtual void changeSize(float l,float w)=0;
virtual double getArea()=0;
virtual bool compareSize(Shape* s0)=0;
};
class Rectangular:public Shape{
public:
Rectangular():Shape(){}
Rectangular(float l,float w):Shape(l,w){}
void display();
void changeSize(float l,float w);
double getArea();
bool compareSize(Shape* s0);
};
class Circle:public Shape{
public:
Circle():Shape(){}
Circle(float r):Shape(r){}
void display();
void changeSize(float l,float w);
double getArea();
bool compareSize(Shape* s0);
};
#endif
==================================================================================================
t3.cpp
#include
#include "t3.h"
using namespace std;
Shape::Shape(){length=0;width=0;radius=0;}
Shape::Shape(float r){radius=r;}
Shape::Shape(float l,float w){length=l;width=w;}
Shape::~Shape(){
cout<<"I was dead."<<endl;
}
float Shape::getRadius(){
return radius;
}
float Shape::getLength(){
return length;
}
float Shape::getWidth(){
return width;
}
//Shape end
void Rectangular::display(){
cout<<"I'm a rectangular."<<endl;
}
void Rectangular::changeSize(float l,float w){
length=length+l;
width=width+w;
}
double Rectangular::getArea(){
return length*width;
}
bool Rectangular::compareSize(Shape* s0){
return this->getArea()>s0->getArea();
}
//Rectangular end
void Circle::display(){
cout<<"I'm a circle."<<endl;
}
void Circle::changeSize(float l,float w=0){
radius=radius+l;
}
double Circle::getArea(){
return 3.14*radius*radius;
}
bool Circle::compareSize(Shape* s0){
return this->getArea()>s0->getArea();
}
//Circle end
int main(){
Rectangular r1=Rectangular();
r1.display();
cout<<"My area
is:"<<r1.getArea()<<endl;
r1.changeSize(5,6);
r1.display();
cout<<"My area
is:"<<r1.getArea()<<endl;
Rectangular r2=Rectangular(3,4);
r2.display();
cout<<"My area
is:"<<r2.getArea()<<endl;
Circle c1=Circle();
c1.display();
cout<<"My area
is:"<<c1.getArea()<<endl;
c1.changeSize(5);
c1.display();
cout<<"My area
is:"<<c1.getArea()<<endl;
Circle c2=Circle(6);
c2.display();
cout<<"My area
is:"<<c2.getArea()<<endl;
Shape *p;
p=&c2;
cout<<r2.compareSize(p)<<endl;;
}
相关文章推荐
- C++程序运行时内存布局之----------无继承情况下的虚函数
- c++涉及继承和虚继承时的内存布局
- 走进C++程序世界------多重继承
- C++ 标准库 vector list map 基本使用方法 示例程序
- 改善C++程序的建议:语法篇1<从C继承而来的特性>
- C++程序运行时内存布局之----------无继承情况下的虚函数
- 走进C++程序世界------继承和派生
- C++学习(六)——类的学习—PhoneList对象数组程序示例
- C++读取txt示例程序
- C++精进篇(九)之―继承及经典示例
- C++继承与虚函数,抽象类的使用
- 【c++程序】成员函数的重载(示例)
- Java调用C/C++程序示例
- Java,C#,C++在继承,覆盖和多态,抽象类等几个方面的比较归纳
- C++ 学习笔记(15)面向对象程序设计(类、继承、虚函数、抽象类、using命令、容器与继承)
- 关于C++对象继承中的内存布局示例详解
- 1Java程序通过JNI调用本地C++_第一个示例_HelloWorld
- 【2016-12-3】C++控制台第一个程序示例
- c++涉及继承和虚继承时的内存布局
- [C/C++] C语言文件读写示例程序 xcb