lzth.net
当前位置:首页 >> C++编写 shApE类 >>

C++编写 shApE类

#include using namespace std;#define PI 3.14enum Colors{ Black, White, Orange, Blue // etc};class Shape{public: inline int Color() const {return color;} void SetColor(int color) {this->color = color;} virtual double Area() const=0;private: int color;};

简单写下了:#include <iostream> using namespace std;class shape{ //定义抽象类public: virtual void show(){ }};class circle:public shape{public: void show(){//输出圆 cout<<"circle"<<endl;}};class rectangle:public shape{ public: void

#include <iostream> using namespace std; class CShape { public: CShape(void) { } ~CShape(void) { } virtual void area(void) = 0; virtual void cir(void) = 0; }; class CRectangle:public CShape { public: CRectangle(int xx = 0,int yy = 0) { x = xx; y = yy; }

简单写下了:#include <iostream> using namespace std; class shape{ //定义抽象类 public: virtual void show(){ } }; class circle:public shape{ public: void show(){//输出圆 cout<<"circle"<<endl; } }; class rectangle:public shape{ public: void show(

abstract class shape {public shape(){} public float getperim(){return 0;} } class rectangle extends shape { private float x=0,y=0; public rectangle(float x,float y){ this.x=x; this.y=y; } public float getperim() { return 2*(x+y); } } class circle extends shape {

class Shape{public:Shape();virtual ~Shape();virtual void draw() = 0;virtual void resize() = 0;private:};Shape::Shape(){}Shape::~Shape(){}class Point : public Shape{public:Point();virtual ~Point();virtual void draw();virtual void resize();private:};Point::Point

<p>Shape定义了变量area,子类却有同名的函数,发生冲突.</p> <p>Shape应定义area虚函数,而不是变量:</p> class Shape{public: double Area() = 0; //纯虚函数};//class Circle: public Shape{ double R;public: double Area(){ return (Area of circle); }};

shape.h#ifndef __SHAPE_H#define __SHAPE_H#includeclass Shape {public: double area() { return 0; } double girth() { return 0; } void show() { std::cout using namespace std;int main() { Triangle tri; Rectangle rect; Circle cir; double a,b,c; cout>a>>

#include<iostream>using namespace std;class Shape{public: virtual double peri()=0;};class Rect : public Shape{ double length; double width;public: Rect(double len=2,double w=1){ length=len; width=w; } double peri(){ return 2*(length+width); }};void main(){ Rect r(1, 1); Shape *p=&r; cout<< p->peri();}

额,这个百度的到吧,梯形的计算面积方法是(上底+下底)* 高 / 2 (小学数学知识呵呵)三角形的 底*高/2圆形的 pi*r*r写成代码的话类似这样,具体就不写详细代码了,写个伪代码class Shape {};#define pi 3.14class CCircle : public Shape{ public mianji() { return pi*r*r; }};}其他类似

网站首页 | 网站地图
All rights reserved Powered by www.lzth.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com