C++.Homework.Polymorphism.01


============TDshape.h===========

#pragma once
#include <iostream>
using namespace std;
class TDshape{
public:
	virtual double area() const=0;
	virtual void printName() const=0;
};

===========Triangle.h=============

#include "TDshape.h"
class Triangle:public TDshape{
	double width,height;
public:
	Triangle(double Width=0,double Height=0):width(Width),height(Height) {};
	double area() const{return (width*height)/2;};
	void printName() const{cout<<"Triangle\n";};
	void setWidth(double Width) {width=Width;};
	void setHeight(double Height) {height=Height;};
	double getWidth() const{return width;};
	double getHeight() const{return height;};
};

==========Rectangle.h=============

#include "TDshape.h"
class Rectangle:public TDshape{
	double width,height;
public:
	Rectangle(double Width=0,double Height=0):width(Width),height(Height) {};
	double area() const{return width*height;};
	void printName() const{cout<<"Rectangle\n";};
	void setWidth(double Width) {width=Width;};
	void setHeight(double Height) {height=Height;};
	double getWidth() const{return width;};
	double getHeight() const{return height;};
};

=========Interface.h===============

#include "Rectangle.h"
#include "Triangle.h"
void interface(TDshape &s){
	s.printName();
	cout<<"The area is:"<<s.area()<<endl;
}

============main.cpp============

#include "Interface.h"
void main(){
	double temp;

	Triangle T(4,5);
	interface(T);
	cout<<"Set Width:";
	cin>>temp;
	T.setWidth(temp);
	cout<<"Set Height:";
	cin>>temp;
	T.setHeight(temp);
	cout<<"Height:"<<T.getHeight()<<"\nWidth:"<<T.getWidth()<<endl;
	interface(T);
	cout<<"======================================================\n";
	Rectangle R(5,6);
	interface(R);
	cout<<"Set Width:";
	cin>>temp;
	R.setWidth(temp);
	cout<<"Set Height:";
	cin>>temp;
	R.setHeight(temp);
	cout<<"Height:"<<R.getHeight()<<"\nWidth:"<<R.getWidth()<<endl;
	interface(R);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值