2-1-2.c++基礎(chǔ)

第一個c++程序

/**
* the is my first c++ program
*/
#include <iostream>
int main(int argc,char* argv[]){
    std::cout<<"hello world"<<std::endl;
    return 0;
}

編譯

clang++ -std=c++11 -g -o hello helloworld.cpp

運行

./hello

第二個c++程序
Human.h

#include <iostream>

class Human{
    public:
        Human(){
            std::cout<<"構(gòu)造函數(shù)"<<std::endl;
            age = 0;
            sex = 0;
        }
        ~Human(){
            std::cout<<"destruct"<<std::endl;
        }
    public:
        void setAge(int a);
        int getAge();
        
        void setSex(int s);
        int getSex();
    private: 
        int age;
        int sex;
};

Human.cpp

#include <iostream>
#include "Human.h"
void Human::setAge(int a){
    age = a;
}

int Human::getAge(){
    return age;
}

void Human::setSex(int s){
    sex = s;
}

int Human::getSex(){
    return sex;
}

main.cpp

#include <iostream>
#include "Human.cpp"
int main(int argc,char* argv[]){
    //棧
    Human human;
    human.setAge(10);
    human.setSex(0);
    std::cout<<human.getAge()<<human.getSex()<<std::endl;

    //堆,不釋放資源,未執(zhí)行destruct
    Human* human1 = new Human();
    human1->setAge(8);
    human1->setSex(0);
    std::cout<< human1->getAge()<< human1->getSex() <<std::endl;
}
clang++ -std=c++11 -g -o main main.cpp
./main
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容