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

简介: 贺老师的教学链接  本课讲解用指针输出几何体#include <iostream>using namespace std;class Point{public: Point(double x=0,double y=0); friend ostream & operator<<(ostream &,const Point

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


用指针输出几何体

#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;
}


目录
相关文章
|
2月前
|
C++
C++ 语言异常处理实战:在编程潮流中坚守稳定,开启代码可靠之旅
【8月更文挑战第22天】C++的异常处理机制是确保程序稳定的关键特性。它允许程序在遇到错误时优雅地响应而非直接崩溃。通过`throw`抛出异常,并用`catch`捕获处理,可使程序控制流跳转至错误处理代码。例如,在进行除法运算或文件读取时,若发生除数为零或文件无法打开等错误,则可通过抛出异常并在调用处捕获来妥善处理这些情况。恰当使用异常处理能显著提升程序的健壮性和维护性。
51 2
|
2月前
|
算法 C语言 C++
C++语言学习指南:从新手到高手,一文带你领略系统编程的巅峰技艺!
【8月更文挑战第22天】C++由Bjarne Stroustrup于1985年创立,凭借卓越性能与灵活性,在系统编程、游戏开发等领域占据重要地位。它继承了C语言的高效性,并引入面向对象编程,使代码更模块化易管理。C++支持基本语法如变量声明与控制结构;通过`iostream`库实现输入输出;利用类与对象实现面向对象编程;提供模板增强代码复用性;具备异常处理机制确保程序健壮性;C++11引入现代化特性简化编程;标准模板库(STL)支持高效编程;多线程支持利用多核优势。虽然学习曲线陡峭,但掌握后可开启高性能编程大门。随着新标准如C++20的发展,C++持续演进,提供更多开发可能性。
49 0
|
6天前
|
编译器 API C语言
超级好用的C++实用库之跨平台实用方法
超级好用的C++实用库之跨平台实用方法
19 6
|
10天前
|
编译器 C++
【C++核心】指针和引用案例详解
这篇文章详细讲解了C++中指针和引用的概念、使用场景和操作技巧,包括指针的定义、指针与数组、指针与函数的关系,以及引用的基本使用、注意事项和作为函数参数和返回值的用法。
14 3
|
10天前
|
C++
【C++案例】一个项目掌握C++基础-通讯录管理系统
这篇文章通过一个通讯录管理系统的C++项目案例,详细介绍了如何使用C++实现添加、显示、删除、查找、修改和清空联系人等功能。
13 3
|
6天前
|
BI 定位技术 C++
超级好用的C++实用库之地理相关接口
超级好用的C++实用库之地理相关接口
14 0
|
22天前
|
JavaScript 前端开发 Java
通过Gtest访问C++静态、私有、保护变量和方法
通过Gtest访问C++静态、私有、保护变量和方法
20 0
|
22天前
|
JavaScript 前端开发 测试技术
一个google Test文件C++语言案例
这篇文章我们来介绍一下真正的C++语言如何用GTest来实现单元测试。
14 0
|
2月前
|
C++
C++ 避免多重定义的方法
C++ 避免多重定义的方法
41 0
|
17天前
|
编译器 C++
C++ 类构造函数初始化列表
构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式。
60 30
下一篇
无影云桌面