一、对象作为函数的返回值
#include<iostream>
#include"circle.h"
int main()
{
Circle c{ 1.0 };
std::cout << c.setRadius(2.0).setRadius(3.0).getArea() << std::endl;
}
结果如上图所示。
二、相关.c
#include"circle.h"
Circle::Circle()
{
radius = 1.0;
}
Circle::Circle(double radius_)
{
radius = radius_;
}
double Circle::getArea()
{
return (3.14 * radius * radius);
}
double Circle::getRadius() const
{
return radius;
}
Circle& Circle::setRadius(double radius)
{
this->radius = radius;
return (*this);
}
三、相关.h
#pragma once
class Circle
{
private:
double radius;
public:
Circle();
Circle(double radius_);
double getArea();
public:
double getRadius() const;
Circle& setRadius(double radius);
};