Rectangular.h
#pragma once
class Rectangular
{
public:
Rectangular(double width, double height);
double& showArea();
void calculateArea();
Rectangular();
~Rectangular();
double m_s;
private:
double m_w;
double m_h;
};
Rectangular.cpp
#include "stdafx.h"
#include "Rectangular.h"
#include <iostream>
using namespace std;
void Rectangular::calculateArea(){
m_s = m_h * m_w;
}
Rectangular::Rectangular(double width, double height){
m_w = width;
m_h = height;
}
double& Rectangular::showArea(){
cout << "多边形面积是:" << m_s << endl;
return m_s;
}
Rectangular::Rectangular()
{
}
Rectangular::~Rectangular()
{
}
Triangle.h
#pragma once
#include "Rectangular.h"
class Triangle :public Rectangular
{
public:
Triangle();
Triangle(double width, double height);
~Triangle();
};
Triangle.cpp
#include "stdafx.h"
#include "Triangle.h"
Triangle::Triangle(double width, double height):Rectangular(width,height){
}
Triangle::Triangle()
{
}
Triangle::~Triangle()
{
}
main.cpp
// 继承-23期-92-子羽.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "Triangle.h"
#include "Rectangular.h"
int _tmain(int argc, _TCHAR* argv[])
{
Rectangular rec = Rectangular(10, 20);
rec.calculateArea();
rec.showArea();
Triangle tri = Triangle(1,2);
tri.calculateArea();
tri.m_s = tri.m_s / 2;
tri.showArea();
return 0;
}