1. 程式人生 > 其它 >教你實現一對一直播系統原始碼自定義圖形層次系統

教你實現一對一直播系統原始碼自定義圖形層次系統

實現一對一直播系統原始碼自定義圖形層次系統使用的相關程式碼

#include<iostream>
#include<iomanip>
const float pai=3.14;
using namespace std;
class Shape
{
public:
virtual float has_s()=0;
virtual float has_v()=0;
};
class Circle:public Shape
{
private:
int r;
public:
Circle(){}
Circle(int x){r=x;}
void input(int a){r=a;}
virtual float has_s(); virtual float has_v(){return 0;} }; float Circle::has_s() { return r*r*pai; } class Square:public Shape { private: int a; public: Square(){} Square(int); void input(int x){a=x;} //virtual void input(int x)=0; virtual float has_s(){return a*a;} virtual float has_v(){return 0;} int get_a(); }; Square::Square(
int x) { a=x; } int Square::get_a() { return a; } class Cube:public Square { public: Cube(){} Cube(int x):Square(x){} //cube 和 square 之間賦值用建構函式還是成員函式input virtual float has_s() { int m=Square::get_a(); return m*m*6; } virtual float has_v() { int n=Square::get_a(); return n*n*n; } //Square input(); }; class
Rectangle:public Shape { private: int a; int b; public: Rectangle(){} Rectangle(int x,int y){a=x;b=y;} void input(int x,int y){a=x;b=y;} virtual float has_s(){return a*b;} virtual float has_v(){return 0;} }; int main() { Shape* p[4]; cout<<"請輸入圓的半徑,它將也是正方形的邊長、立方體的邊長、矩形的寬"<<endl; int a; cin>>a; p[0]=new Circle(a); cout<<"s of a circle:"<<std::left<<setw(5)<<p[0]->has_s()<<" v of a circle:"<<p[0]->has_v()<<endl; p[1]=new Square(a); cout<<"s of a square:"<<std::left<<setw(5)<<p[1]->has_s()<<" v of a square:"<<p[1]->has_v()<<endl; p[2]=new Cube(a); cout<<"s of a cube:"<<std::left<<setw(5)<<p[2]->has_s()<<" v of a cube:"<<p[2]->has_v()<<endl; cout<<"請輸入矩形的‘長’"<<endl; int b; cin>>b; p[3]=new Rectangle(a,b); cout<<"s of a rectangle:"<<std::left<<setw(5)<<p[3]->has_s()<<" v of a rectangle:"<<p[3]->has_v()<<endl; return 0; //int //p[1] }

編寫一個程式,定義抽象基類Shape,由它派生出3個派生類:Circle(圓形),Square(正方形),Rectangle(矩形)。Square正方形派生出了cube正方體。
用虛擬函式分別計算幾種圖形面積或體積,並求用基類指標陣列,使它每一個元素指向一個派生類物件。

1.繼承也可以有多繼承,一個派生類繼續向下繼承,成為另一個類的基類。
這樣情況下,最低端的的派生類仍可以擁有比其高几級的基類的成員,可以直接呼叫public部分,也含有基類私有成員。

2.多重繼承運用純虛擬函式要在基類中使用關鍵字virtual,並使函式=0,表明其在基類中不實現,如果其在派生類中也不實現,則也寫成=0。
以上就是 實現一對一直播系統原始碼自定義圖形層次系統使用的相關程式碼,更多內容歡迎關注之後的文章