个人代购开网店的平台:一个c++作业
来源:百度文库 编辑:神马品牌网 时间:2024/05/13 16:09:18
#include<iostream.h>
#include<string.h>
const double PI=3.1415;
class circle
{
private:
double r,c,area;
public:
circle(double get_r) {r=get_r;}
double get_c() {return 2*PI*r; }
double get_area() {return PI*r*r;}
void print();
};
void circle::print()
{
cout<<"圆桌的圆周为:"<<get_c()<<"米"<<endl;
cout<<"圆桌的面积为:"<<get_area()<<"平方米"<<endl;
}
class table
{
private:
double height;
char colour[20];
public:
table(double get_height,char get_colour[])
{height=get_height;strcpy(colour,get_colour);}
double get_height(){return height;}
void print();
};
void table::print()
{
cout<<"圆桌的高度为:"<<height<<"米"<<endl;
cout<<"圆桌的颜色为:"<<colour<<endl;
}
class roundtable:public circle,public table
{
private:
double price;
public:
roundtable(double get_r,double get_height,char get_colour[],double get_price)
:circle(get_r),table(get_height,get_colour)
{price=get_price;}
double getprice(){return price;}
};
void main()
{
cout<<"********圆桌数据********"<<endl;
roundtable a(0.70,1.50,"red",76.32);
a.circle::print();
a.table::print();
cout<<"圆桌的价钱为:"<<a.getprice()<<endl;
}
该程序已经在VC++6.0运行通过,不过有点复杂,是我写的,原来的课本的那个简单些!!
// 头文件:table.h
#ifndef __ROUNDTABLE_H__
#define __ROUNDTABLE_H__
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#define PI 3.1415
using namespace std;
class circle
{
private:
double r; // 圆半径
double x; // 圆心横坐标
double y; // 圆心纵坐标
double area;// 面积
public:
circle()
: r(0), x(0), y(0)
{
cout << "缺省构造函数被调用" << endl;
cout << "圆半径:" << r << "米, 圆心:(" << x << ", " << y << ")" << endl;
GetArea();
}
circle(const double &R, const double &X=0, const double &Y=0)
: r(R), x(X), y(Y)
{
cout << "构造函数被调用" << endl;
cout << "圆半径:" << r << "米, 圆心:(" << x << ", " << y << ")" << endl;
GetArea();
}
circle(const circle &p)
: r(p.r), x(p.x), y(p.y)
{}
~circle()
{}
public:
static double Abs(double val)
{
return val>=0 ? val : -val;
}
double GetArea()
{
area = (PI * r * r);
cout << "圆半径:" << r << "米, 面积:" << area << "平方米" << endl;
return area;
}
};
class table
{
private:
double h;
string c;
public:
table()
: h((double)1), c("black")
{
cout << "缺省构造函数被调用" << endl;
cout << "桌高:" << h << "米, 颜色:" << c.c_str() << endl;
}
table(const double &H, const string &C="black")
: h(H), c(C)
{
cout << "构造函数被调用" << endl;
cout << "桌高:" << h << "米, 颜色:" << c.c_str() << endl;
}
~table(){}
public:
double Height()
{
return h;
}
string Color()
{
return c;
}
};
class roundtable :
public circle, public table
{
private:
roundtable();
public:
roundtable(const circle &Circle, const table &Table)
: circle(Circle), table(Table)
{
cout << "构造函数1被调用:" << this << endl;
}
roundtable(const double &H, const double &R, const string &C="black", const double &X=0, const double &Y=0)
: circle(R, X, Y), table(H, C)
{
cout << "构造函数2被调用:" << this << endl;
}
~roundtable(){}
public:
};
#endif
// 主程序:table.cpp
#include "table.h"
int main(void)
{
double area, height, test;
string color;
circle a(5);
table b(1.2, "red");
roundtable rt(a, b);
//roundtable rt(1.2, 5, "red");
area = rt.circle::GetArea();
height = rt.table::Height();
color = rt.table::Color();
printf("桌面积:%f平方米, 桌高:%f米, 桌颜色:%s\n", area, height, color.c_str());
return 0;
}