English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

我要寫一個點,圓,圓柱體的座標,(繼承)
‧ Point, the base class, has x and y coordinators as protected data members;
‧ Circle, publicly derived from the base class Point, has an additional protected data member
radius. The objects of class Circle use the (x, y) coordinators of class Point as the center.
‧ Cylinder, publicly derived from class Circle, has an additional protected data member height.

顯示結果如下
X coordinator is 12
Y coordinator is 23
Radius is 2.5
Height is 5.7
The new location, radius, and height of cyl are:
Center = [2, 2]; Radius = 4.25; Height = 10.00
Area (surface) = 380.53; Volume = 567.45
Cylinder printed as a Point is: [2, 2]
Cylinder printed as a Circle is:
Center = [2, 2]; Radius = 4.25
Area: 56.74


程式碼如下
//prob4point.h
#ifndef PROB4POINT_H
#define PROB4POINT_H
#include
using namespace std;
class Point {
friend ostream &operator<<(ostream &, const Point &);
public:
Point(int = 0, int = 0);
void setPoint(int, int);
int getX() const {return x;}
int getY() const {return y;}
protected:
int x, y;
};
#endif
--------------------------------------------------------
//prob4point.cpp
//member function implementation for class Point
#include
#include "prob4point.h"
using namespace std;
/*
這裡要寫(點)
*/
--------------------------------------------------
//prob4circle.h
// Definition of class Circle
#ifndef PROB4CIRCLE_H
#define PROB4CIRCLE_H
#include "prob4point.h"
class Circle : public Point {
friend ostream &operator<<(ostream &, const Circle &);
public:
// default constructor
Circle(double r = 0.0, int x = 0, int y = 0);
void setRadius(double);
double getRadius() const;
double area() const; // calculate area
protected: // accessible to derived classes
double radius; // radius of the Circle
};
#endif
--------------------------------------------------
// prob4circle.cpp
#include
#include
using namespace std;
#include "prob4circle.h"
/*
這裡要寫(圓)
*/
-------------------------------------------------

2006-09-24 09:53:19 · 3 個解答 · 發問者 Mike 1 in 電腦與網際網路 程式設計

// prob4cylinder.h
#ifndef PROB4CYLINDER_H
#define PROB4CYLINDER_H
#include "prob4circle.h"
class Cylinder : public Circle {
friend ostream & operator << (ostream &, const Cylinder &);

2006-09-24 09:54:39 · update #1

public:
// default constructor
Cylinder(double h = 0.0, double r = 0.0, int x = 0, int y = 0);
void setHeight(double); // set height
double getHeight() const; // return height
double area() const; // calculate and return area (surface area
)

2006-09-24 09:56:40 · update #2

double volume() const; // calculate and return volume
protected:
double height; // height of the Cylinder
};
#endif
----

2006-09-24 09:57:04 · update #3

// prob4cylinder.cpp
// Member and friend function definitions for class Cylinder
#include
#include
#include "prob4cylinder.h"
using namespace std;
/*
這裡要寫(圓柱體)
*/
----------------------------------------------

2006-09-24 09:57:27 · update #4

// prob4-main.cpp
#include
#include
#include "prob4point.h"
#include "prob4circle.h"
#include "prob4cylinder.h"
int main()
{
// create cylinder object
Cylinder cyl(5.7, 2.5, 12, 23);
// use get functions to display the cylinder

2006-09-24 09:58:45 · update #5

cout << "X coordinator is " << cyl.getX()
<< "\nY coordinator is " << cyl.getY()
<< "\nRadius is " << cyl.getRadius()
<< "\nHeight is " << cyl.getHeight() << "\n\n";

2006-09-24 10:00:01 · update #6

// use set functions to change the cylinder’s attributes
cyl.setHeight(10);
cyl.setRadius(4.25);
cyl.setPoint(2, 2);
cout << "The new location, radius, and height of cyl are: \n"
<< cyl << endl;

2006-09-24 10:00:18 · update #7

// display the Cylinder as a Point
Point &pRef = cyl; // pRef "thinks" it is a Point
cout << "\nCylinder printed as a Point is: "
<< pRef << "\n\n";
// display the Cylinder as a Circle
// 這裡要寫(顯示)
return 0;
}

2006-09-24 10:00:30 · update #8

Sorry~~程式碼太長的!!麻煩各位了

2006-09-24 10:01:11 · update #9

沒有拉~~希望大家都可以幫幫我!!
謝謝囉~~

2006-09-24 11:21:14 · update #10

可以請大家幫我看另一個程式嗎??
http://tw.knowledge.yahoo.com/question/?qid=1206092406393
是有關Unary Operator Overloading

2006-09-24 13:07:30 · update #11

3 個解答

首先 prob4point.h 檔裡的 int getX() const {return x;} 變成 int getX() const;int getY() const {return y;} 變成 int getY() const;因為後面繼承的類別都是拉到外面來定義的,並非刻意改變。//prob4point.cpp#include #include "prob4point.h"using namespace std;ostream& operator << (ostream& out, const Point &p){out<<"["<#include #include "prob4circle.h"using namespace std;ostream& operator << (ostream& out, const Circle &p){out<<"Center = ["< #include #include "prob4cylinder.h" using namespace std;ostream& operator << (ostream& out, const Cylinder &p){out<<"Center = ["<answer #1 · answered by Almond 6 · 0 0

這個好像是課本的習題耶?!
看到這個題目我就懶得去寫…(我很懶)

2006-09-24 13:04:01 · answer #2 · answered by Big_John-tw 7 · 0 0

那... 你的問題是什麼? 只希望 ASD 回答的話, 你應該寄信給他~~

2006-09-24 11:19:57 · answer #3 · answered by Rody 5 · 0 0

fedest.com, questions and answers