面向过程和面向对象demo

来源:互联网 发布:唱歌测音软件 编辑:程序博客网 时间:2024/05/17 03:17
#include<iostream>#include<string>using namespace std;inline int max(int,int);inline double max(double a,double b);class Human{public:string hair;string clothes;string body;Human(){}void speak(){cout<<"The person can speak."<<endl;} void walk(void){cout<<"The person can walk."<<endl;}};int main(){int a=1,b=2;double c=3,d=4;int *const aq=&a,*const bq=&b;double &cx=c,&dx=d;cout<<"int max:\t"<<max(*aq,*bq)<<endl;cout<<"double max:\t"<<max(cx,dx)<<endl;Human human;human.hair="black";human.clothes="blue";human.body="thin";cout<<human.hair+" "<<human.clothes+" "<<human.body+" "<<endl;human.speak();human.walk();cout<<endl;Human *man=new Human();if(man==NULL){cout<<"error"<<endl;return 0;}man->hair="red";man->clothes="green";man->body="fat";cout<<(man->hair)+" "<<(man->clothes)+" "<<(man->body)+" "<<endl;man->speak();man->walk();delete man;man=NULL;cin.get();return 0;}int max(int a,int b=0){if(a>=b){return a;}else{return b;}}double max(double a,double b=0.0){switch(a>=b){case true:return a;break;case false:return b;default:std::cout<<"illegal result"<<endl;break;}}

原创粉丝点击