C++类和对象实例解析(二)
C++既是面向对象也是面向过程的语言,在这里就有一个重要的概念――类。 类是一个抽象,它不占任何内存,只有当其实例化成为对象之后,才会给其分配空间。 下面通过实例理解 【2-1】定义一个日C++类和对象期类date,该类对象存放一个日期,可以提供的操作有:取日期值,取年份,提取月份,提取日期以及设计日期值。 void getdate(); //取日期值,格式为“year-month-day” int getyear(); //取年份 int getmonth(); //取月份 int getday(); //取日期 void setdate(int y,int m,int d); //设置日期值 程序代码如下: #include <iostream> using namespace std; class date { public: void getdate(); int getyear(); int getmonth(); int getday(); void setdate(int y,int d); private: int year,day; }; int date::getyear(){ return year; } int date::getmonth(){ return month; } int date::getday(){ return day; } void date::getdate() { cout<<"today is:"<<year<<"-"<<month<<"-"<<day<<endl; } void date::setdate(int y,int d) { year=y; month=m; day=d; } int main() { date d; d.setdate(2011,2,1); cout<<"year="<<d.getyear()<<endl; cout<<"month="<<d.getmonth()<<endl; cout<<"day="<<d.getday()<<endl; d.getdate(); return 0; } 【总结】这是一个最简单的类程序,但其中也存在不少问题。比如,如果没有setdate()函数,就无法对对象赋初值,而且必须按照特定的格式对对象进行设置初值。这个问题就应该采用构造函数来进行对象初始化。 #include <iostream> using namespace std; class date { public: date(int y=0,int m=0,int d=0) { year=y; month=m; day=d; } date(date &d) { year=d.year; month=d.month; day=d.day; } void getdate() { cout<<"today is:"<<year<<"-"<<month<<"-"<<day<<endl; } int getyear() { return year; } int getmonth() { return month; } int getday() { return day; } private: int year,day; }; int main() { date d1(2011,1),d2,d3(d1); d1.getdate(); d2.getdate(); d3.getdate(); return 0; } 【总结】程序中的对象d1有完整的实参表,则调用构造函数来进行对象初始化;对象d2没有实参表,系统同样自动调用构造函数进行对象初始化,只不过形参的值采用默认参数值进行初始化。对于对象d3,则采用拷贝构造函数进行初始化。 #include <iostream> using namespace std; #define N 10 class array { public: void input(); void output(); int max(); int min(); int sum(); float average(); private: int a[N],maxnumber,minnumber,sumnumber; float avenumber; }; void array::input() { int i=0; cout<<"Please input "<<N<<" numbers:"<<endl; for(i=0;i<N;i++) { cout<<"a["<<i<<"]="; cin>>a[i]; } } void array::output() { int i=0; cout<<"array a is:"<<endl; for(i=0;i<N;i++) cout<<a[i]<<" "; cout<<endl; } int array::max() { int i; maxnumber=a[0]; for(i=1;i<N;i++) if(maxnumber<a[i]) maxnumber=a[i]; return maxnumber; } int array::min() { int i; minnumber=a[0]; for(i=1;i<N;i++) if(minnumber>a[i]) minnumber=a[i]; return minnumber; } int array::sum() { int i; sumnumber=a[0]; for(i=1;i<N;i++) sumnumber+=a[i]; return sumnumber; } float array::average() { float ave=static_cast<float>(sumnumber/N); return ave; } int main() { array a; a.input(); a.output(); cout<<"The max number is "<<a.max()<<endl; cout<<"The min number is "<<a.min()<<endl; cout<<"The sum number is "<<a.sum()<<endl; cout<<"The average number is"<<a.average()<<endl; return 0; } 【总结】从上面的程序可以看出,类array里面包括对数组的各种操作,包括求和,求最大值,求最小值以及求平均值等。 #include <iostream> #include <string> using namespace std; class Cat { public: Cat(string c="red",float w=1,int a=1); string get_color(); float get_weight(); int get_age(); void display(); private: string color; float weight; int age; }; Cat::Cat(string c,float w,int a) { color=c; weight=w; age=a; } string Cat::get_color() { return color; } float Cat::get_weight() { return weight; } int Cat::get_age() { return age; } void Cat::display() { cout<<"The color of this cat is "<<get_color()<<endl; cout<<"The weight of this cat is "<<get_weight()<<endl; cout<<"The age of this cat is "<<get_age()<<endl; } int main() { Cat c1,c2("yellow",1,2); cout<<"c1 is:"<<endl; c1.display(); cout<<"c2 is:"<<endl; c2.display(); return 0; } 【总结】从上面的程序可以看出,构造函数可以用类对对象进行初始化,并且构造函数可以进行重载。 希望本文可以对大家学习c++编程语言有所帮助,也希望大家可以继续关注! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |