加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c++下学期全部

发布时间:2020-12-16 07:19:45 所属栏目:百科 来源:网络整理
导读:89 成员函数:重载函数 返回值是Time类对象Time Time:: operator ++() /// /返回值是该Time类的一个对象 { ++ second; if (second= 60 ) { second =second- 60 ; ++ minute; } return * this ; // 返回当前对象值 Time类对象 } 构造函数: 没有返回值,函数名

8&&9

成员函数:
重载函数 返回值是Time类对象
Time Time::operator++()////返回值是该Time类的一个对象  
{
    ++second;
    if(second>=60)
    {
        second=second-60;
        ++minute;
    }
    return *this;//返回当前对象值 Time类对象
}

构造函数:
没有返回值,函数名就是类名 Time::Time(
int hour) { ... }

?

?
 

 

8章 
void set_time(Time &t,int hour=0,int minute=0,int second=0);//声明指定了默认参数
void set_time(Time &t,int hour,int minute,int second)//定义时不再指定默认参数,若指定了则要跟声明一样,否则以声明为主

 
 
Box(int h=10,int w=10,int len=10);//声明构造函数时指定默认参数  Box::Box(int h,int w,int len)//定义构造函数时不指定默认参数

//构造函数初始化成员(带参数 有默认参数 带初始化列表的构造函数)   //有默认参数就不用重载了 只写这个! 第10,33-38可合并写在类中: Box(int h=10,int w=10,int len=10): height(h),width(w),length(len) {}; 9章 class Student { public: Student(int a,char b,char name1[]): number(a),sex(b) { strcpy(name,name1); } private: int number; char sex; char name[];//错误char name } int getArea() const //常函数 void fun2(int b) const//常函数 { cout<<b<<" fun2 is called"<<endl; cout<<"width="<<width<<endl;//const函数 非const成员 引用,不能修改 对 }

//指向对象的指针 Time *p2; p2=&t1; p2->get_time(); cout<<endl; //指向函数的指针 //类:Time //Time类函数指针: void (Time::*p3) (); 即p3 指向Time类任意一个函数的指针 //Time类对象: Time t1 //Time类函数:void Time::get_time() void (Time::*p3) ();//定义指向Time类成员函数的指针变量! p3=&Time::get_time;//错误1 p3=&Time::get_time(); 错误2 p3=&t1.grt_time; 指针指向Time类函数void Time::get_time() (t1.*p3) ();//!!!最后回归对象 t1 !! 由t1调用函数 cout<<endl;  Box *p; p=new Box(1,2,3); cout<<"p->volume="<<p->volume()<<endl; cout<<"p->a="<<p->a<<endl; delete p; /* int* element;// element为int的指针 element=new int[5]; delete []element;//数组要用delete 否则内存泄漏! */ //指向对象的常指针  //0.对象定义时不用初始化 //1.指针定义时可初始化 或 不初始化,只能指向一个对象,不能指向其他对象,指针值不能修改 //2.被指的对象的成员值可以修改,可以通过指针值来修改 //3.常用于函数参数 //4.Data *const p=&d; //指向常对象的指针 //1.对象是常对象,只能用该指针,且不能修改(本来); //2.对象不是常对象,能修改,但不可以通过指针值来修改 //3.定义时不用初始化,指针值是可以改变的 //4.const Data *p; //指向常变量的指针 //情况1 const char c[]="boy";//const变量 const char    *p1; char *p2; p1=c;// p2=c;//错 p2不是指向const变量的指针 //情况2 char c1=a;//不是const变量 const char      *p; p=&c1; *p=b;//错  c1=b;////指向常对象的指针 //情况2 //常用!!! Time t1(10,12,15); const Time     *p=&t1; t1.hour=18;// (*p).hour=18;// int main() { void fun(const Time    *p);//指向常对象的指针作为函数参数 Time t1(10,13,56); fun(&t1);//要取地址! 错:fun(t1); 常引用的才这样写 return 0; } void fun(const Time *p) { p->hour=18;// cout<<p->hour<<endl; } //对象的常引用 void fun(Time &t)//对,非 常引用, 能修改 { t.hour=18; } void fun2(const Time &t)//错误,常引用不能修改 { //t.minute=18;// } //const Data &r //Data (const Data &r): x(r.x),y(r.y),data(r.data){}//复制构造函数 用 常引用作为唯一的形参    Student stud[4]= { Student(001,18,90),Student(002,19,95),Student(003,20,99),Student(004,99)//!!! };
void display(Time &t)//普通函数。形参是Time类对象的引用 { cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;//要t.hour 不能hour } //Time类display函数 用到  Date类的变量 要提前引用声明 //Date类中 声明 Time中 display函数为 友元函数   要先定义Time类!后定义Date类 class Date;//提前引用声明 class Time { public: Time(int h,int m,int s):hour(h),minute(m),second(s){} void display(Date &d); class Date { public: Date(int y,int d):year(y),month(m),day(d){} friend void Time::display(Date &d);//void不能省略! //因为,37行 用到 13行的函数,这决定了要先定义Time类,后定义Data类! /*错误 //要将display定义放在Date声明之后,因为display要用到Date中的变量year month day void display(Date &d)//这个是Time的成员函数,形参为Date类对象 { cout<<d.month<<"/"<<d.day<<"/"<<d.year<<endl; cout<<hour<<":"<<minute<<":"<<second<<endl; } ***/ int main() { Time t1(23,14,56); Date d1(2017,11,30); t1.display(d1);//Date类中 声明 Time中 display函数为 友元函数 return 0; }

template
<class numtype> class Compare { public: Compare(numtype a,numtype b) {x=a,y=b;} } 等价于 template<class T> class Compare { public: Compare(T a,T b) {x=a,y=b;} } 等价于 template<typename T> class Compare { public: Compare(T a,y=b;} } 另外 template<typename T,int n> class Compare { public: Compare(T a,int b) {x=a,y=b;} }


/* template<typename T> T Compare<T>::min() */ template<typename T,int n> void A<T,n>::SetElement(int index,const T& value) { element[index]=value;//初始化数组数据 }



class Student { public://!!!!!! static int average()//静态函数 { return sum/count; } } static int sum;//静态成员 int Student::sum=0;////静态成员初始化 int Student::count=0;//静态成员初始化 cout<<"the average score of"<<n<<"students is"<<Student::average()<<endl;///由类名调用成员函数

?

?
?

?10

 
  
10章
/*
作为友元:
双目运算符+、cin、cout

其他作为成员函数
++...
*/



//不用运算符+重载 //成员函数 不是构造函数 不是重载函数 Complex complex_add(Complex &c2);//返回值是该类的一个对象 Complex Complex::complex_add(Complex &c2) { Complex c; c.real=real+c2.real;// c.imag=imag+c2.imag; return c; } c3=c1.complex_add(c2); ////运算符+重载 Complex operator +(Complex &c2);// Complex Complex::operator+ (Complex &c2)// 必须是引用,否则报错 { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } c3=c1+c2; c4=c1.operator+(c2);


//
运算符+重载 //双目+ //作为 成员函数 class Complex { public: Complex operator +(Complex &c2);// void display(); private: double real,imag; }; Complex Complex::operator+ (Complex &c2)// 必须是引用,否则报错 { Complex c; c.real=real+c2.real; c.imag=imag+c2.imag; return c; } c3=c1+c2; c4=c1.operator+(c2); //运算符函数重载 作为 友元 非成员函数 //双目+ friend Complex operator+(Complex &c1,Complex &c2); Complex operator+(Complex &c1,Complex &c2) { Complex c3; c3.imag=c1.imag+c2.imag; c3.real=c1.real+c2.real; return c3; } c3=c1+c2; c4=operator+(c1,c2); //运算符函数重载 作为 类成员函数  正确 //双目+ Complex operator+(int &i); Complex Complex::operator+(int &i)//也是要引用用的! { Complex c3; c3.imag=imag; c3.real=real+i; return c3; } c3=c1+i; c4=c1.operator+(i); //运算符函数重载作为 友元 非成员函数 ////双目+ friend Complex operator+(int i,Complex c2); Complex operator+(int i,Complex c2)//只能声明为非成员函数 { Complex c3; c3.imag=c2.imag; c3.real=i+c2.real; return c3; } c3=i+c1;//!!!!!! c4=operator+(i,c1);

//重载双目运算符 string类定义 class String { String(char *str); void display(); }; //重载双目运算符 重载双目运算符> //作为 友元 非成员函数 //双目> friend bool operator>(String &string1,String &string2);// bool operator>(String &string1,String &string2) { if( strcmp(string1.p,string2.p)>0 )//在本例子中,string1.p指向Hello,string2.p指向Book,用strcmp比较 return 1; return 0; } String string1("Hello"),string2("Book"); int bool1= string1>string2; void compare(String &string1,String &string2) { if( string1>string2 )//operator>(string1,string2) ==1 { string1.display(); cout<<">"; string2.display(); } //单目 前置自增++ Time operator++(); Time Time::operator++()////返回值是该Time类的一个对象 { ++second; if(second>=60) { second=second-60; ++minute; } return *this;//返回当前对象值 Time类对象 }
/*
第23至32若不用this 可改用下面的
Time Time::operator++()//没有参数
{
Time t1;
++second;//second 是当前对象的成员
if(second>=60)
{
second=second-60;
++minute;
}

t1.second=second;
t1.minute=minute;

}
*/

//单目 后置自增++ Time operator++(); Time operator++(int); Time Time::operator++(int) { Time temp(*this);//临时对象//Time t1(t2) 对象复制! second++; if(second>=60) { second=second-60; minute++; } return temp;//返回自增前的对象 ////重载流插入运算符<< friend ostream &operator<<(ostream & output,Complex &c); ostream &operator<<(ostream & output,Complex &c) //重载流提取运算符>> class Complex { public: friend ostream& operator<<(ostream& output,Complex &c);//output 自定,可定为os friend istream& operator>>(istream& input,Complex &c);//input自定,可定为is { ostream& operator<<(ostream& output,Complex &c) { output<<"("<<c.real; if(c.imag>=0) output<<"+"; output<<c.imag<<"i)"<<endl; return output; } istream& operator>>(istream& input,Complex &c) { cout<<"input real part and imaginary part of complex number:"; input>>c.real>>c.imag; return input; } //类型转换函数 Complex(double r,double i): real(r),imag(i){} output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl; return output; } d1=2.5+c1;//隐式的  (double) c1没写出来 cout<<"d1="<<d1<<endl; (double)c2;//显示的  d2=2+c2; cout<<"d2="<<d2<<endl; //用运算符重载(要两个复数相加) //用转换构造函数 #include<iostream> using namespace std; class Complex { public: Complex():real(0),imag(0){} Complex(double r):real(r),imag(0){}//转换构造函数! 将double型转换为Complex类 Complex(double r,double i):real(r),imag(i){}//用运算符重载 friend Complex operator+(Complex c1,Complex c2); c3=c1+2.5;//更好!!隐式的 c3=c1+Complex(2.5);没写出来 c3.display(); double d1=5.5; c4=c1+Complex(d1);//显式的 c4.display(); //用类型转换函数 #include<iostream> using namespace std; class Complex { public: Complex():real(0),imag(0){} Complex(double r,double i):real(r),imag(i){} operator double(){return real;} d1=c1+2.5;//隐式的  d2=2.5+c1; cout<<"d1="<<d1<<endl<<"d2="<<d2<<endl; (double)c2;//显式的 double(c2);报错 d3=2.5+c2; cout<<"d3="<<d3<<endl;

?

?上课补充

学生类{对象A、对象B}{数据成员:学号 成员函数:学习}

?

教师类{对象a、对象b}{工号、教学}

?

(this->x)++;//不要this->x++;

(this->y)++;

?

学期小结(next task)

没过ppt,

但是过了抄写部分,13章不熟悉

delete、new完成,类模板完成,

丽丽那份课后题没看

vector一章课后题没看,不会,以后用到再来

要记忆的次序调用不熟悉!

?欠 13二进制文件之后的

?

/* 以下大部分懂了

考试题目:
元角分 !!!赋值运算函数, 重载- << >>
用类模板定义一位数组,可以返回指定位置的元素,entry();

new delete涉及析构函数,构造函数,先子类,再基类,调用次序

继承 派生涉及的析构函数 构造函数 ppt自己写输出再校对

从一个文件搬到另一个文件,抄写!
用while()
..........?
out.put(ch);

另外只要
默写写熟内容
将以上涉及到的ppt过一遍,课本例子,外加题目将其输出自己默写一遍,必要加入抄写,指针一节课后题,笔记再整里,

其他没涉及到的ppt、以及丽丽的题目过一遍
*/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读