赞
踩
构造函数是C++提供的对类的对象进行初始化的函数,它不需要用户调用,在对象建立时自动执行,构造函数可重载;P246
而析构函数与构造函数作用相反,是在对象生命周期结束自动执行的在对象内存撤销前完成的清理工作(不是删除对象),一个类只能有一个析构函数。P255
构造函数,提高了对对象初始化的效率,主要作用就是用户按照需求初始化,即使用户没有定义构造函数,系统也会自动生成空的构造函数;析构函数主要用于资源释放,对象最后一次调用的操作等,即使用户没有定义系统也会生成空的析构函数。
#include<iostream> using namespace std;//印刷少了一行! class Date{ public: Date(int,int ,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) {}//参数初始化表初始化 Date::Date(int m,int d):month(m),day(d){ year=2005; } //指定默认值 Date::Date(int m):month(m){ day=1; year=2005; } Date::Date(){ month=1; day=1; year=2005; } void Date::display() { cout<<month<<"/"<<day<<"/"<<year<<endl; } int main(){ Date d1(10,13,2005); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; } /* 显然输出是: 10/13/2005 12/30/2005 10/1/2005 1/1/2005 */
//声明构造函数时指定默认参数是可以的,但全部默认参数构造函数会与构造函数重载产生歧义,所以这样显然是错误的P255页(4)条 //修改,直接删掉其它构造函数的重载即可,这样在传入参数按照参数值否则按照默认值,就可以得到和上一问相同的结果 #include<iostream> using namespace std; class Date{ public: Date(int =1,int =1,int =2005); /* Date(int,int); Date(int); Date();*/ void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) {}//参数初始化表初始化 /*Date::Date(int m,int d):month(m),day(d){ year=2005; } //指定默认值 Date::Date(int m):month(m){ day=1; year=2005; } Date::Date(){ month=1; day=1; year=2005; }*/ void Date::display() { cout<<month<<"/"<<day<<"/"<<year<<endl; } int main(){ Date d1(10,13); Date d2(12,30); Date d3(10); Date d4; d1.display(); d2.display(); d3.display(); d4.display(); return 0; }
#include<iostream> using namespace std; class Student{ public: Student(int n,double s):num(n),score(s){} void display(); private: int num; double score; }; void Student::display() { cout<<"student:"<<num<<", score:"<<score<<endl; } int main(){ Student stu[5]={Student(1001,85),Student(1002,97),Student(1003,78),Student(1004,91),Student(1005,88.5)}; Student *p; p=&stu[0]; (*p).display(); p=p+2; (*p).display(); p=p+2; (*p).display(); return 0; }
#include<iostream> using namespace std; class Student{ public: Student(int n,double s):num(n),score(s){} void display(); double sc(); private: int num; double score; }; void Student::display() { cout<<"student:"<<num<<", score:"<<score<<endl; } double Student::sc(){ return score; } void max(Student *p){ double max; max=(*p).sc();//用一个公有成员函数得到score for(int i=0;i<5;i++){ if((*p).sc()>max) max=(*p).sc(); p++; } cout<<"max="<<max; } int main(){ Student stu[5]={Student(1001,85),Student(1002,97),Student(1003,78),Student(1004,91),Student(1005,88.5)}; Student *p; p=&stu[0]; max(p); return 0; }
#include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display(){ cout<<num<<" "<<score<<endl; } void change(int n,float s){ num=n; score=s; } private: int num; float score; }; int main(){ Student stud(101,78.5); stud.display() ; stud.change(101,80.5); stud.display(); return 0; } /* 显然输出: 101 78.5 101 80.5 */
//(1) #include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display(){ cout<<num<<" "<<score<<endl; } void change(int n,float s){ num=n; score=s; } private: int num; float score; }; int main(){ const Student stud(101,78.5);//如果对象定义为常对象,不可被普通成员函数调用,无法运行 stud.display() ; stud.change(101,80.5); stud.display(); return 0; }
//(2) P267 #include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display() const{ cout<<num<<" "<<score<<endl; }//必须声明为常成员函数 void change (int n,float s) const{//声明为常成员函数 num=n; score=s; } private: mutable int num;//如果一定要改变常对象的值,声明为mutable mutable float score; }; int main(){ const Student stud(101,78.5); stud.display() ; stud.change(101,80.5); stud.display(); return 0; }
//(3) #include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display() { cout<<num<<" "<<score<<endl; } void change (int n,float s){ num=n; score=s; } private: int num; float score; }; int main(){ Student stud(101,78.5); Student *p=&stud; //指向对象的指针 p->display() ;//与stud.display();等价 p->change(101,80.5); p->display(); return 0; }
//(4) #include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display() const{ cout<<num<<" "<<score<<endl; } void change (int n,float s){ num=n; score=s; } private: int num; float score; }; int main(){ Student stud(101,78.5); const Student *p=&stud; //指向常对象的指针 ,指向非const对象,不能通过指针改变指向对象 p->display() ;//而要通过指针调用成员函数,成员函数必须声明为const ,即不可修改本类的成员 stud.change(101,80.5);//对象本身可以改变,不可通过指针改变对象 p->display(); return 0; }
//(5) #include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display(){ cout<<num<<" "<<score<<endl; } void change (int n,float s){ num=n; score=s; } private: int num; float score; }; int main(){ Student stud(101,78.5); Student *const p=&stud; //指向对象的常指针,只是指针的指向不能改变 p->display() ; p->change(101,80.5);//可以通过指针改变对象 p->display(); return 0; }
#include<iostream> using namespace std; class Student{ public: Student(int n,float s):num(n),score(s){} void display(){ cout<<num<<" "<<score<<endl; } void change(int n,float s){ num=n; score=s; } private: int num; float score; }; void fun(Student &s){ s.display(); s.change(101,80.5); s.display(); } int main(){ Student stud(101,78.5); fun(stud); return 0; }
//参考P283计算学生平均成绩 #include<iostream> using namespace std; class Sale{ public: Sale(int n,int q,double p){ num=n; quantity=q; price=p; } void sumsale(){//公有成员函数既可以访问静态成员也可以访问非静态成员 if(quantity<=10){ sum=quantity*price*discount; } else{ sum=quantity*price*0.98*discount; } n=quantity; } static double average(){ //静态成员函数只访问静态数据成员不可访问非静态成员,(要访问必须限定对象,那么一定要将对象定义放在前面) return sum/n; } static void display(){ cout<<"average:"<<average()<<endl; } private: int num; int quantity; double price; static double sum; static int n; static double discount; }; //一般情况下,静态成员函数访问静态数据成员,公有成员函数可以对所有数据成员进行处理 double Sale::discount=0.9;//静态成员只能在类外初始化,属于类 double Sale::sum=0.0; int Sale::n=0; int main(){ Sale s1(101,5,23.5); Sale s2(102,12,24.56); Sale s3(103,100,21.5); s1.sumsale(); cout<<"No 1: "; s1.display(); s2.sumsale(); cout<<"No 2: "; s2.display(); s3.sumsale(); cout<<"No 3: "; s3.display(); return 0; }
//原程序 #include<iostream> using namespace std; class Date ; class Time{ public: Time(int,int,int); void display(Date &); private: int hour; int minute; int sec; }; class Date{ public: Date(int,int,int); friend void Time::display(Date &); private: int month; int day; int year; }; Time::Time(int h,int m,int s){ hour=h; minute=m; sec=s; } void Time::display(Date &d){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; } Date::Date(int m,int d,int y){ month=m; day=d; year=y; } int main(){ Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0; }
//修改 #include<iostream> using namespace std; class Date ; class Time{ public: Time(int,int,int); friend void display(Date &,Time &); private: int hour; int minute; int sec; }; class Date{ public: Date(int,int,int); friend void display(Date &,Time &); private: int month; int day; int year; }; Time::Time(int h,int m,int s){ hour=h; minute=m; sec=s; } void display(Date &d,Time &t){//普通函数声明为友元 cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl; } Date::Date(int m,int d,int y){ month=m; day=d; year=y; } int main(){ Time t1(10,13,56); Date d1(12,25,2004); display(d1,t1); return 0; }
#include<iostream> using namespace std; class Date; class Time{ public: Time(int,int,int); void display(Date &d);//友元类形参仍因该是Date类对象的引用 private: int hour; int minute; int sec; }; class Date{ public: Date(int,int,int); friend Time;//Time声明为Date的友元类,Time中所有成员函数可以访问Date成员 private: int month; int day; int year; }; Time::Time(int h,int m,int s){ hour=h; minute=m; sec=s; } void Time::display(Date &d){ cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<sec<<endl; } Date::Date(int m,int d,int y){ month=m; day=d; year=y; } int main(){ Time t1(10,13,56); Date d1(12,25,2004); t1.display(d1); return 0; }
//原程序 //P292 例9.14类模板 #include<iostream> using namespace std; template <class numtype>//声明类模板,虚拟类型名numtype class Compare{ public: Compare(numtype a,numtype b){ x=a; y=b; } numtype max(){//类里定义成员函数 return (x>y)?x:y; } numtype min(){ return (x>y)?y:x; } private: numtype x,y; }; int main(){ Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the maximum of the two integers."<<endl; cout<<cmp1.min()<<" is the minimum of the two integers."<<endl<<endl; Compare<float> cmp2(47.8,96.34); cout<<cmp2.max()<<" is the maximum of the two float numbers."<<endl; cout<<cmp2.min()<<" is the minimum of the two float numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" is the maximum of the two characters."<<endl; cout<<cmp3.min()<<" is the minimum of the two characters."<<endl<<endl; return 0; }
//修改 #include<iostream> using namespace std; template <class numtype>//声明类模板,虚拟类型名numtype class Compare{ public: Compare(numtype a,numtype b){ x=a; y=b; } numtype max(); numtype min(); private: numtype x,y; }; //template <class numtype> numtype Compare<numtype>::max(){//类外定义成员函数 template <class numtype>//类模板 numtype Compare<numtype>::max(){//注意,第一个numtype是函数的类型,Compare<numtype>是一个整体代表带参的类 return (x>y)?x:y; } template <class numtype> numtype Compare<numtype>::min(){ return (x>y)?y:x; } int main(){ Compare<int> cmp1(3,7); cout<<cmp1.max()<<" is the maximum of the two integers."<<endl; cout<<cmp1.min()<<" is the minimum of the two integers."<<endl<<endl; Compare<float> cmp2(47.8,96.34); cout<<cmp2.max()<<" is the maximum of the two float numbers."<<endl; cout<<cmp2.min()<<" is the minimum of the two float numbers."<<endl<<endl; Compare<char> cmp3('a','A'); cout<<cmp3.max()<<" is the maximum of the two characters."<<endl; cout<<cmp3.min()<<" is the minimum of the two characters."<<endl<<endl; return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。