zoukankan      html  css  js  c++  java
  • c++面向对象解圆的面积

    c++第一天
    //相当于对圆进行了一个类型抽象
    //数据类型的本质:固定大小内存块的别名
    //在结构体里面可以封装属性和函数
    struct Circle {
        double r;//属性--》成员变量
        double s;
        setR(double m_r) {//方法--》成员函数
            r = m_r;
        }
        void printfR() {
            printf("r:%d", r);
        }
        double getS() {
            s = 3.14*r%r;
            return s;
        }
        
    };
    int main() {
        Circle cl;//用类型定义变量 只会分配内存,不会主动的去调用类的函数
        double r = 0;
        cout << "请输入圆的半径";
        cin >> r;
        c1.setR(r);//把r设置到圆变量中
        c1.getS();
        c1.printfR();
        cout << "圆的面积" << c1.getS() << endl;
    }
    完整例子:
    #include <iostream>
    using namespace std;
    struct Circle {
        double r;
        double s;
        void setR(double m_r) {
            r = m_r;
        }
        void printfR() {
            cout << "r: " << r << endl;
        }
        double getS() {
            s = 3.14*r*r;
            return s;
        }
        void printfS() {
            cout << "s: " << s << endl;
        }
    };

    int main() {
        Circle circle;
        double r;
        cout << "请输入半径r的值:" << endl;
        cin >> r;
        circle.setR(r);
        circle.printfR();
        circle.getS();
        circle.printfS();
        system("pause");
        return 0;
    }

  • 相关阅读:
    让tomcat启动时,自动加载你的项目
    ssh整合 小例子
    hibernate入门(二)
    java引用问题(—)
    hibernate入门(-)
    IOC入门1
    百度知道回答的依赖注入
    spring
    ibatis 优点,未完版
    Data Structure Array: Sort elements by frequency
  • 原文地址:https://www.cnblogs.com/dongjian16/p/6958526.html
Copyright © 2011-2022 走看看