design a Circle class and create its objectives

The bsisc codes for C++:

1. I use the bot operation to invoke the property value and its function ; get the following outputs:

output the default value of raidus5.2
output the setting value of raidus7.8

* the first output is use the constructors function without any input arguments 

* the second output use the constructors function with an double argument, please notice the recall methods in the following corresponding codes: 

//objective: know the basic creation  and uses for class and its corresponding objective iterm 
#include <iomanip>
#include <iostream>
int main()
{
    class Circles{
        public: double radius;
        //constructors function(no any return values; with same name of class)
        Circles(){
            radius = 5.2;
        }
        Circles(double len){
            radius = len;
        }
        double areas(){
            return radius * radius * 3.14159265;
        }
    };
    Circles c_1;
    std::cout << "output the default value of raidus" << c_1.radius << std::endl;
    Circles c_2{7.8};
    std::cout << "output the setting value of raidus" << c_2.radius << std::endl;
    return 0;
}

I know you must have notice the above codes has a function called areas. It is obvious that this function could give you the area of the circle. The following codes will show you how to invoke this function by its objectives:

outputs:

output the default value of raidus5.2
output the areas of circles with raidus c_1: 84.9487
output the setting value of raidus7.8
output the areas of circles with radius c_2: 191.134

The corresponding codes:

//objective: know the basic creation  and uses for class and its corresponding objective iterm 
#include <iomanip>
#include <iostream>
int main()
{
    class Circles{
        public: double radius;
        //constructors function(no any return values; with same name of class)
        Circles(){
            radius = 5.2;
        }
        Circles(double len){
            radius = len;
        }
        double areas(){
            return radius * radius * 3.14159265;
        }
    };
    Circles c_1;
    std::cout << "output the default value of raidus" << c_1.radius << std::endl;
    std::cout << "output the areas of circles with raidus c_1: " << c_1.areas() << std::endl;
    Circles c_2{7.8};
    std::cout << "output the setting value of raidus" << c_2.radius << std::endl;
    std::cout << "output the areas of circles with radius c_2: " << c_2.areas() << std::endl;
    return 0;
}

 The following codes may be more suitable to the standard outputs:

The default radius of the circle_1 is: 1
The radius of the now circle_1 is: 4.6
The area of the now circle_1 is: 66.476
The default radius of the circle_2 is: 5.6
The area of the now circle_2 is: 98.5203

The corresponding codes:

//objective: know the basic creation  and uses for class and its corresponding objective iterm 
//use the code framework in the videos I have seen before 
#include <iostream>
#include <cmath>
//define a Circle class in the global area
class Circle{
    public:
        double radius;
        Circle(){
            radius = 1.0;
        }
        Circle(double newRadius){
            radius = newRadius;
        }
        double getArea(){
            return pow(radius, 2.0) * 3.14159;
        }
};
int main()
{
    Circle circle_1;
    std::cout << "The default radius of the circle_1 is: " << circle_1.radius << std::endl;
    // change the radius of the circle_1 to 4.6
    circle_1.radius = 4.6;
    std::cout << "The radius of the now circle_1 is: " << circle_1.radius << std::endl;
    std::cout << "The area of the now circle_1 is: " << circle_1.getArea() << std::endl;

    Circle circle_2{5.6};
    std::cout << "The default radius of the circle_2 is: " << circle_2.radius << std::endl;
    std::cout << "The area of the now circle_2 is: " << circle_2.getArea() << std::endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值