============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);
}