C++作业题,高手进来啊!!!悬赏100分
来源:百度知道 编辑:UC知道 时间:2024/05/31 06:55:49
编程要点:1, Class的概念2, Function 的概念3, 在base class 中使用虚函数Virutal的概念4, 在class中使用构造,析构函数的使用。#include <iostream.h>
Class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b) { width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area (void){ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area (void){ return (width * height / 2); }
};
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << en
在.cpp文件中输入:
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
#include "aa.h"
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (5,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
cout << rect.perimeter() << endl;
cout << trgl.perimeter() << endl;
return 0;
}
创建.h文件,文件名为aa.h
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b) { width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area (void){ return (width * height);}
int perimeter (void){return (4*width);}
};
class CTriangle: public CPolygon {
public:
float area (void){ return (width * width*sqrt(3)/4); }
int perimeter (void){return