C++语言基础 例程 案例:一个接口,多种方法

贺老师的教学链接  本课讲解

用指针输出几何体

#include <iostream>
using namespace std;

class Point
{
public:
    Point(double x=0,double y=0);
    friend ostream & operator<<(ostream &,const Point &);
protected:
    double x,y;
};

Point::Point(double a,double b):x(a), y(b){}

ostream & operator<<(ostream &output,const Point &p)
{
    output<<"  点:["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    double area ( ) const;
    friend ostream &operator<<(ostream &,const Circle &);
protected:
    double radius;
};

Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { }
double Circle::area( ) const
{
    return 3.14159*radius*radius;
}
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"  圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
    return output;
}

class Cylinder:public Circle
{
public:
    Cylinder (double x=0,double y=0,double r=0,double h=0);
    double area( ) const;
    friend ostream& operator<<(ostream&,const Cylinder&);
protected:
    double height;
};

Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {}

double Cylinder::area( ) const
{
    return 2*Circle::area( )+2*3.14159*Circle::radius*height;
}

ostream &operator<<(ostream &output,const Cylinder& cy)
{
    output<<"  圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height
          <<",area="<<cy.area( )<<endl;
    return output;
}

int main( )
{
    Cylinder cy1(3.5, 6.4, 5.2, 10);
    cout<<cy1<<endl;

    Point *p=&cy1;
    cout<<"用指向点的指针输出:"<<endl;
    cout<<(*p);

    Circle *c= &cy1;
    cout<<"用指向圆的指针输出:"<<endl;
    cout<<(*c);

    Cylinder *cy= &cy1;
    cout<<"用指向圆柱的指针输出:"<<endl;
    cout<<(*cy)<<endl;
    return 0;
}

用引用输出几何体

#include <iostream>
using namespace std;

class Point
{
public:
    Point(double x=0,double y=0);
    friend ostream & operator<<(ostream &,const Point &);
protected:
    double x,y;
};

Point::Point(double a,double b):x(a), y(b){}

ostream & operator<<(ostream &output,const Point &p)
{
    output<<"  点:["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    double area ( ) const;
    friend ostream &operator<<(ostream &,const Circle &);
protected:
    double radius;
};

Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { }

double Circle::area( ) const
{
    return 3.14159*radius*radius;
}
ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"  圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
    return output;
}

class Cylinder:public Circle
{
public:
    Cylinder (double x=0,double y=0,double r=0,double h=0);
    double area( ) const;
    friend ostream& operator<<(ostream&,const Cylinder&);
protected:
    double height;
};

Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {}

double Cylinder::area( ) const
{
    return 2*Circle::area( )+2*3.14159*Circle::radius*height;
}

ostream &operator<<(ostream &output,const Cylinder& cy)
{
    output<<"  圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height
          <<",area="<<cy.area( )<<endl;
    return output;
}

int main( )
{
    Cylinder cy1(3.5, 6.4, 5.2, 10);
    cout<<cy1<<endl;

    cout<<"使用引用:"<<endl;
    Point &pRef=cy1;
    cout<<"用点的引用输出:"<<endl;
    cout<<pRef<<endl;

    Circle &cRef=cy1;
    cout<<"用圆的引用输出:"<<endl;
    cout<<cRef<<endl;

    Cylinder &cyRef=cy1;
    cout<<"用圆柱的引用输出:"<<endl;
    cout<<cyRef<<endl;
    return 0;
}

输出几何体面积

#include <iostream>
using namespace std;

class Point
{
public:
    Point(double x=0,double y=0);
    friend ostream & operator<<(ostream &,const Point &);
protected:
    double x,y;
};

Point::Point(double a,double b):x(a), y(b){}

ostream & operator<<(ostream &output,const Point &p)
{
    output<<"  点:["<<p.x<<","<<p.y<<"]"<<endl;
    return output;
}

class Circle:public Point
{
public:
    Circle(double x=0,double y=0,double r=0);
    double area ( ) const;
    friend ostream &operator<<(ostream &,const Circle &);
protected:
    double radius;
};

Circle::Circle(double a,double b,double r):Point(a,b),radius(r) { }

double Circle::area( ) const
{
    return 3.14159*radius*radius;
}

ostream &operator<<(ostream &output,const Circle &c)
{
    output<<"  圆:Center=["<<c.x<<","<<c.y<<"],r="<<c.radius<<",area="<<c.area( )<<endl;
    return output;
}

class Cylinder:public Circle
{
public:
    Cylinder (double x=0,double y=0,double r=0,double h=0);
    double area( ) const;
    friend ostream& operator<<(ostream&,const Cylinder&);
protected:
    double height;
};

Cylinder::Cylinder(double a,double b,double r,double h):Circle(a,b,r),height(h) {}

double Cylinder::area( ) const
{
    return 2*Circle::area( )+2*3.14159*Circle::radius*height;
}

ostream &operator<<(ostream &output,const Cylinder& cy)
{
    output<<"  圆柱:Center=["<<cy.x<<","<<cy.y<<"],r="<<cy.radius<<",h="<<cy.height
          <<",area="<<cy.area( )<<endl;
    return output;
}

int main( )
{
    Cylinder cy1(3.5, 6.4, 5.2, 10);

    Circle *c= &cy1;
    Cylinder *cy= &cy1;

    cout<<"关于面积:"<<endl;
    cout<<"用指向圆的指针输出:"<<endl;
    cout<<c->area()<<endl;
    cout<<"用指向圆柱的指针输出:"<<endl;
    cout<<cy->area()<<endl<<endl;
    return 0;
}
时间: 2024-09-30 07:02:03

C++语言基础 例程 案例:一个接口,多种方法的相关文章

C++语言基础 例程 案例:bmp文件格式剖析

贺老师的教学链接  本课讲解 附:二进制文件查看器及示例bmp文件 http://pan.baidu.com/s/1dDjf5uD 用程序读出BMP文件信息 //readbmp.h #ifndef READBMP_H_INCLUDED #define READBMP_H_INCLUDED typedef unsigned char BYTE; typedef unsigned short int UINT; typedef short int WORD; typedef int DWORD; t

C++语言基础 例程 案例:MyVector类的设计

贺老师的教学链接  本课讲解 //MyVector类的设计 #include <iostream> using namespace std; class MyVector //定义向量类 { public: MyVector(int m); //构造函数,共有m个元素的向量,元素值预置为0 MyVector(const MyVector &v); //复制构造函数 ~MyVector(); //析构函数:释放动态数组所占用的存储空间 friend istream &operat

C++语言基础 例程 案例:Time类的设计

贺老师的教学链接  本课讲解 #include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 时 unsigned short int minute; // 分 unsigned short int second; // 秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); //输

C++语言基础 例程 二进制文件应用案例

贺老师的教学链接  本课讲解 系统升级第一步:转换现有数据格式(附:数据文件点击打开链接) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; typedef struct { int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total; } Stude

C++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 标准输入流

贺老师的教学链接  本课讲解 例: 输入个数不确定的成绩 #include <iostream> using namespace std; int main( ) { float grade; cout<<"enter grade:"; while(cin>>grade)//能从cin流读取数据 { if(grade>=85) cout<<grade<<" GOOD!"<<endl; if

C++语言基础 例程 字符串流

贺老师的教学链接  本课讲解 例:"写"字符数组 #include<iostream> #include <strstream> using namespace std; struct student { int num; char name[20]; float score; }; int main( ) { student stud[3]= {1001,"Li",78,1002,"Wang",89.5,1004,&qu

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 类声明和成员函数定义的分离

贺老师的教学链接  本课讲解 1.一个程序,一个源文件的做法 #include<iostream> #include<cstring> using namespace std; class Student { private: char Name[20]; //学生姓名 double Chinese; //语文成绩 double Math; //数学成绩 public: double Average( );//计算平均成绩 double Sum( ); //计算总分 void Sh