c++物件導向解圓的面積

來源:互聯網
上載者:User

標籤:面向   資料類型   pac   iostream   name   out   return   完整   sys   

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

c++物件導向解圓的面積

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.