定义一个rectangle 类,它包含两个数据成员 length 和 width ;以及包含用于求长方形面积的成员函数。

再定义 rectangle 的派生类 rectangular, 它包含一个新数据成员height 和
用来求长方体体积的成员函数。在函数main()中,使用2个类,求某个长方形的面积和某个长方体的体积。

#include<stdio.h>

class Rectangle

{

public:

double Length;//长度

double Width;//宽度

Rectangle(double length, double width) {//定义一个有两个参数的构造函数,用于设置长方形的宽度和长度

this->Length = length;

this->Width = width;

}

double Area() { //求面积函数

return Width * Length;//返回长度和宽度的乘积

}

};

class Rectangular :Rectangle

{

public:

double Height;

Rectangular(double length, double width, double height) :Rectangle(length, width) {//定义一个有三个参数的构造函数,用于设置立方体的宽度和长度以及高度

this->Height = height;

}

double Volume() {//求体积函数

return this->Area() * Height;//返回长度和宽度及高度的乘积

}

};

int main() {

Rectangle rect(3, 6);//定义长方体类实例

printf("面积:%lf \n", rect.Area());//调用求面积方法并输出结果

Rectangular rguar(5, 10, 3);//定义立方体类实例

printf("体积:%lf", rguar.Volume());//调用求体积方法并输出结果

char c;

scanf("%c", &c);//这句是为了防止控制台退出

return 0;

}

public class TestRec{

public static void main(String[] args){

double vol, area = 0.0;

Rectangle rec = new Rectangle(2, 3);

area = rec.getRecArea();

Rectangular recr = new Rectangular();

recr.setLength(2);

recr.setWidth(3);

recr.setHeight(4);

vol = recr.getRecVol();

System.out.println("长方形面积:" + area + ",长方体体积:" + vol);

}

}

扩展资料:

数据成员 m_hDC CDC对象使用的输出设备上下文。

m_hAttribDC CDC对象使用的输出属性上下文。

构造函数 CDC 构造一个CDC对象。

初始化 CreateDC 为指定设备创建设备上下文。

CreateIC 为指定设备创建信息上下文。这提供了一种不创建设备上下文即获取有关设备信息的快速方式。

CreateCompatibleDC 创建内存设备上下文,与另一个设备上下文匹配。可以用它在内存中准备图像。

DeleteDC 删除CDC对象对应的Windows设备上下文。

FromHandle 给定设备上下文句柄时,返回指向CDC对象的指针。如果CDC对象未附加到句柄,则创建并附加一个临时CDC对象。

参考资料来源:百度百科-成员函数

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-26

    定义矩形类;

    声明长和宽两个属性;

    写出计算面积的函数。

class Rectangle{
    private double length, width;
    public Rectangle(){}
    public Rectangle(double length, double width){
        this.length = length;
        this.width = width;
    }
    public void setLength(double length){
        this.length = length;
    }
    public double getLength(){
        return this.length;
    }
    public void setWidth(double width){
        this.width= width;
    }
    public double getWidth(){
        return this.width;
    }
    //计算面积的函数
    public double getRecArea(){
        return length * width;
    }
}

class Rectangular extend Rectangle{
    private double height;
    public void setHeight(double height){
        this.height = height;
    }
    public double getHeight(){
        return height;
    }
    //计算体积的函数
    public double getRecVol(){
        return super.getLength * super.getWidth * height;
    }
}

public class TestRec{
    public static void main(String[] args){
        double vol, area = 0.0;
        Rectangle rec = new Rectangle(2, 3);
        area = rec.getRecArea();
        Rectangular recr = new Rectangular();
        recr.setLength(2);
        recr.setWidth(3);
        recr.setHeight(4);
        vol = recr.getRecVol();
        System.out.println("长方形面积:" + area + ",长方体体积:" + vol);
    }
}

第2个回答  2017-09-16
#include <iostream>
using namespace std;
class rectangle
{
public:
//构造函数
rectangle(int length, int width)
{
this->length = length;
this->width = width;
}
//求面积
void area()
{
cout << "面积:" << length*width << endl;
}
protected:
//长和宽
int length;
int width;
};


int main()
{

rectangle  object(100, 200);
object.area();


system("pause"); 
return 0;
}

第3个回答  2017-08-18
#include<stdio.h>
class Rectangle
{
public:
double Length;//长度
double Width;//宽度
Rectangle(double length, double width) {//定义一个有两个参数的构造函数,用于设置长方形的宽度和长度
this->Length = length;
this->Width = width;
}

double Area() { //求面积函数
return Width * Length;//返回长度和宽度的乘积
}
};

class Rectangular :Rectangle
{
public:
double Height;
Rectangular(double length, double width, double height) :Rectangle(length, width) {//定义一个有三个参数的构造函数,用于设置立方体的宽度和长度以及高度
this->Height = height;
}
double Volume() {//求体积函数
return this->Area() * Height;//返回长度和宽度及高度的乘积
}
};

int main() {
Rectangle rect(3, 6);//定义长方体类实例
printf("面积:%lf \n", rect.Area());//调用求面积方法并输出结果
Rectangular rguar(5, 10, 3);//定义立方体类实例
printf("体积:%lf", rguar.Volume());//调用求体积方法并输出结果
char c;
scanf("%c", &c);//这句是为了防止控制台退出
return 0;
}

以上是在Visual Studio 里写的C++语言例子

按题目要求写了上述代码,由于程序功能跟要求相同我就不再赘述了。

第4个回答  2015-07-06

/* .h文件  头文件*/
#ifndef __RECTANGLE_H__
#define __RECTANGLE_H__
class rectangle
{
public:
    rectangle(double fLength, double fWidth);
    ~rectangle(void);

private:
    double      m_fLength; 
    double      m_fWidth; 

public:
    double      Area();
};

class rectangular : public rectangle
{
public:
    rectangular(double fLength, double fWidth, double fHeight);
    ~rectangular();

private:
    double      m_fHeight;


public:
    double      Volume();
};

#endif

/* .cpp文件  实现文件 */

#include "rectangle.h"

rectangle::rectangle(double fLength, double fWidth)
{
    m_fLength = fLength;
    m_fWidth = fWidth;
}


rectangle::~rectangle(void)
{
}

double rectangle::Area()
{
    return m_fLength * m_fWidth;
}

rectangular::rectangular(double fLength, double fWidth, double fHeight):
rectangle(fLength, fWidth)
{
    m_fHeight = fHeight;
}

rectangular::~rectangular()
{

}

double rectangular::Volume()
{
    return Area() * m_fHeight;
}

#include<stdio.h>
#include "rectangle.h"
int main(void)
{
    rectangle regle(10,20);
    rectangular relar(3,4,5);

    printf("Area = %.2f\nVolume=%.2f\n", regle.Area(), relar.Volume());
    return 0;
}

本回答被网友采纳