第一个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<<"构造函数"<<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;
//堆,不释放资源,未执行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