尼玛,转眼两个月过去了,C++课翘了那么多次,然后翘着翘着,马上就期末考试了,听说是11月28号,可是各种类啊,对象啊,还没搞清楚是啥回事,好不容易想去上课了,尼玛睡过了...
今天把程序过一遍吧,过两天再看看概念,应该没事....
//******************************
// 第一章 to 第六章
// 。。。。。。。。。。。
//******************************
几个点意思一下就行了
//***********************
// 第七章 类与对象
//***********************
// 模拟时钟
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
void settime(int h,int m,int s)
{
H=(h>=0&&h<24)?h:0;
M=(m>=0&&m<60)?m:0;
S=(s>=0&&s<60)?s:0;
}
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
};
clock myclock;
int main()
{
myclock.showtime();
myclock.settime(8,30,30);
myclock.showtime();
return 0;
}
// 构造函数与析构函数
//通过这个程序可以看出析构函数的执行顺序
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
clock(int h=0,int m=0,int s=0)
{
H=h,M=m,S=s;
cout<<"constructor:"<<H<<":"<<M<<":"<<S<<endl;
}
~clock()
{
cout<<"destructor:"<<H<<":"<<M<<":"<<S<<endl;
}
};
int main()
{
clock c3(10,0,0);
clock c4(11,0,0);
return 0;
}
clock c1(8,0,0);
clock c2(9,0,0);
// 字符串类与对象
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char *str;
int len;
public:
void showstr()
{
cout<<"string:"<<str<<",length:"<<len<<endl;
}
String()
{
len=0;
str=NULL;
}
String(const char *p)
{
len=strlen(p);
str=new char[len+1];
strcpy(str,p);
}
~String()
{
if(str!=NULL)
{
delete [] str;
str=NULL;
}
}
};
int main()
{
char s[]="ABCDE";
String s1(s);
String s2("12345");
s1.showstr();
s2.showstr();
return 0;
}
//带拷贝函数的始终类
//
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
clock(int h=0,int m=0,int s=0)
{
H=h,M=m,S=s;
cout<<"constructor:"<<H<<":"<<M<<":"<<S<<endl;
}
~clock()
{
cout<<"destructor:"<<H<<":"<<M<<":"<<S<<endl;
}
clock(clock &p)
{
cout<<"copy constructor,before call:"<<H<<":"<<M<<":"<<S<<endl;
H=p.H;
M=p.M;
S=p.S;
}
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
};
clock fun(clock c)
{
return c;//用已存在的对象c建立临时对象,调用拷贝
}
int main()
{
clock c1(8,0,0);
clock c2(9,0,0);
clock c3(c1);
fun(c2);
clock c4;
c4=c2;
return 0;
}
//带时间加法的时钟类
//
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
void settime(int h,int m,int s)
{
H=h;
M=m;
S=s;
}
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
clock(int h=0,int m=0,int s=0)
{
H=h;M=m;S=s;
}
clock(clock &p)
{
H=p.H;
M=p.M;
S=p.S;
}
void timeadd(clock *cp);
void timeadd(int h,int m,int s);
void timeadd(int s);
};
void clock::timeadd(clock *cp)
{
H=(cp->H+H+(cp->M+M+(cp->S+S)/60)/60)%24;
M=(cp->M+M+(cp->S+S)/60)%60;
S=(cp->S+S)%60;
}
void clock::timeadd(int h,int m,int s)
{
H=(h+H+(m+M+(s+S)/60)/60)%24;
M=(m+M+(s+S)/60)%60;
S=(S+s)%60;
}
void clock::timeadd(int s)
{
H=(H+(M+(S+s)/60)/60)%24;
M=(M+(S+s)/60)%60;
S=(S+s)%60;
}
int main()
{
clock c1;
clock c2(8,20,20);
c1.timeadd(4000);
c1.showtime();
c2.timeadd(&c1);
c2.showtime();
return 0;
}
//总评成绩的一个score类
//好SB的程序,运行效率也低
#include <iostream>
using namespace std;
const int maxn=100;
const double rate=0.6;
class Score
{
private:
long No;
char *Name;
int Peace;
int Final;
int Total;
public:
Score(long =0,char * =NULL,int =0,int =0,int =0);
void count();
void showscore();
};
Score::Score(long no,char *name,int peace,int final,int total)
{
No=no;
Name=name;
Peace=peace;
Total=total;
Final=final;
}
void Score::count()
{
Total=Peace*rate+(1-rate)*Final+0.5;
return ;
}
void Score::showscore()
{
cout<<No<<"\t"<<Name<<"\t\t"<<Peace<<"\t"<<Final<<"\t"<<Total<<endl;
}
int main()
{
Score classscore1[3];
Score classscore2[3]={Score(200607001,"LiuNa",80,79),
Score(200607002,"CuiPeng",90,85),Score(200607003,"ZhouJun",70,55)};
for(int i=0;i<3;i++)
classscore2[i].count();
for(int i=0;i<3;i++)
classscore2[i].showscore();
return 0;
}
//计算火车旅程的组合类
//
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
void settime(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
clock(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
int geth()
{
return H;
}
int getm()
{
return M;
}
int gets()
{
return S;
}
};
class traintrip
{
private:
char *trainno;
clock starttime;
clock endtime;
public:
traintrip(char * trainno,clock S,clock E)
{
this->trainno=trainno;
starttime=S;
endtime=E;
}
clock triptime()
{
int tH,tM,tS;
int carry=0;
clock ttime;
(tS=endtime.gets()-starttime.gets())>0?carry=0:tS+=60,carry=1;
(tM=endtime.getm()-starttime.getm()-carry)>0?carry=0:tM+=60,carry=1;
(tH=endtime.geth()-starttime.geth()-carry)>0?carry=0:tH+=24;
ttime.settime(tH,tM,tS);
return ttime;
}
};
int main()
{
clock c1(8,10,10),c2(6,1,2);
clock c3;
traintrip t1("K16",c1,c2);
c3=t1.triptime();
c3.showtime();
return 0;
}
//含有对象计数器的学生类
#include <iostream>
#include <cstring>
using namespace std;
class student
{
private:
char *name;
int no ;
static int counts;
public:
static int getcount()
{
return counts;
}
student(char* ="",int =0);
student(student &);
~student();
};
student::student(char *name,int no)
{
this->name=new char [strlen(name)+1];
strcpy(this->name,name);
this->no=no;
++counts;
cout<<"constructing:"<<name<<endl;
}
student::student(student &r)
{
name=new char [strlen(r.name)+1];
strcpy(name,r.name);
no=r.no;
++counts;
cout<<"copy constructing:"<<r.name<<endl;
}
student::~student()
{
cout<<"destructing:"<<name<<endl;
delete [] name;
--counts;
}
int student::counts=0;
int main()
{
cout<<student::getcount()<<endl;
student s1("Antony");
cout<<s1.getcount()<<endl;
student s2(s1);
cout<<s1.getcount()<<endl;
student s3[2];
cout<<student::getcount()<<endl;
student *s4=new student[3];
cout<<student::getcount()<<endl;
delete [] s4;
cout<<student::getcount()<<endl;
return 0;
}
//计算火车旅途时间的友元函数
#include <iostream>
using namespace std;
class clock
{
private:
int H,M,S;
public:
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
void settime(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
clock(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
friend clock triptime(clock &starttime,clock &endtime);
};
clock triptime(clock &starttime,clock &endtime)
{
int tH,tM,tS;
int carry=0;
clock ttime;
(tS=endtime.S-starttime.S)>0?carry=0:tS+=60,carry=1;
(tM=endtime.M-starttime.M-carry)>0?carry=0:tM+=60,carry=1;
(tH=endtime.H-starttime.H-carry)>0?carry=0:tH+=24;
ttime.settime(tH,tM,tS);
return ttime;
}
int main()
{
clock c1(8,10,10),c2(6,1,2);
clock c3;
c3=triptime(c1,c2);
c3.showtime();
return 0;
}
//计算吗火车旅途时间的友元类
#include <iostream>
using namespace std;
class traintrip;
class clock
{
private:
int H,M,S;
public:
void showtime()
{
cout<<H<<":"<<M<<":"<<S<<endl;
}
void settime(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
clock(int H=0,int M=0,int S=0)
{
this->H=H;
this->M=M;
this->S=S;
}
friend class traintrip;
};
class traintrip
{
private:
char *trainno;
clock starttime,endtime;
public:
traintrip(char *trainno,clock S,clock E)
{
this->trainno=trainno;
starttime=S;
endtime=E;
}
clock triptime()
{
int tH,tM,tS;
int carry=0;
clock ttime;
(tS=endtime.S-starttime.S)>0?carry=0:tS+=60,carry=1;
(tM=endtime.M-starttime.M-carry)>0?carry=0:tM+=60,carry=1;
(tH=endtime.H-starttime.H-carry)>0?carry=0:tH+=24;
ttime.settime(tH,tM,tS);
return ttime;
}
};
int main()
{
clock c1(8,10,10),c2(6,1,2);
clock c3;
traintrip t1("K16",c1,c2);
c3=t1.triptime();
c3.showtime();
return 0;
}
// 常数据成员的使用
//
#include <iostream>
using namespace std;
class A
{
private:
const int &r;
const int a;
static const int b;
public:
A(int i):a(i),r(a)
{
cout<<"constructor!"<<endl;
}
void display()
{
cout<<a<<","<<b<<","<<r<<endl;
}
};
const int A::b=3;
int main()
{
A a1(1);
a1.display();
A a2(2);
a2.display();
return 0;
}
//************************
// 第八章 继承与派生
//************************
//单继承的构造与析构
//
#include <iostream>
using namespace std;
class point
{
private:
int x,y;
public:
point(int x=0,int y=0)
{
this->x=x;
this->y=y;
cout<<"point("<<x<<","<<y<<") constructing..."<<endl;
}
~point()
{
cout<<"point("<<x<<","<<y<<") destructing..."<<endl;
}
};
class circle:protected point
{
protected:
double radius;
public:
circle(double r=0,int x=0,int y=0):point(x,y)
{
radius=r;
cout<<"circle constructing,radius:"<<r<<endl;
}
~circle()
{
cout<<"circle destructing,radius:"<<radius<<endl;
}
};
class tube:protected circle
{
private:
double heigth;
circle incircle;
public:
tube(double h,double r1,double r2=0,int x=0,int y=0):
incircle(r2,x,y),circle(r1,x,y)
{
heigth=h;
cout<<"tube constructing,heigth:"<<h<<endl;
}
~tube()
{
cout<<"tube destructing,heigth:"<<heigth<<endl;
}
};
int main()
{
tube tu(100,20,5);
return 0;
}
//多继承的二义性
//
#include <iostream>
using namespace std;
class car
{
private:
int power;
int seat;
public:
car(int power,int seat)
{
this->power=power;
this->seat=seat;
}
void show()
{
cout<<"car power:"<<power<<" seat:"<<seat<<endl;
}
};
class wagon
{
private:
int power;
int load;
public:
wagon(int power,int load)
{
this->power=power;
this->load=load;
}
void show()
{
cout<<"wagon power:"<<power<<" load:"<<load<<endl;
}
};
class stationwagon:public car,public wagon
{
public:
stationwagon(int power,int seat,int load):wagon(power,load),
car(power,seat)
{
}
void showsw()
{
cout<<"stationwagon:"<<endl;
car::show();
wagon::show();
}
};
int main()
{
stationwagon sw(105,3,8);
sw.showsw();
return 0;
}
//虚基类的构造函数
//
#include <iostream>
using namespace std;
class automobile
{
private:
int power;
public:
automobile(int power)
{
this->power=power;
cout<<"automobile constructing..."<<endl;
}
void show()
{
cout<<" power: "<<power;
}
};
class car:virtual public automobile
{
private:
int seat;
public:
car(int power,int seat):automobile(power)
{
this->seat=seat;
cout<<"car constructing..."<<endl;
}
void show()
{
cout<<"car";
automobile::show();
cout<<" seat:"<<seat<<endl;
}
};
class wagon:virtual public automobile
{
private:
int load;
public:
wagon(int power,int load):automobile(power)
{
this->load=load;
cout<<"wagon constructing..."<<endl;
}
void show()
{
cout<<"wagon:";
automobile::show();
cout<<" load:"<<load<<endl;
}
};
class stationwagon:public car,public wagon
{
public:
stationwagon(int cpower,int wpower,int seat,int load)
:automobile(cpower),wagon(wpower,load),car(cpower,seat)
{
cout<<"station constructing..."<<endl;
}
void show()
{
cout<<"stationwagon:"<<endl;
car::show();
wagon::show();
}
};
int main()
{
stationwagon sw(105,108,3,8);
sw.show();
return 0;
}
//******************
// 第九章 多态性
//******************
//重载+-*/为类的成员函数
//负数运算
//
#include <iostream>
using namespace std;
class Complex
{
private:
double real;
double image;
public:
Complex(double real=0.0,double image=0.0)
{
this->real=real;
this->image=image;
}
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
Complex operator + (Complex B);
Complex operator - (Complex B);
Complex operator - ();
Complex operator ++ ();
Complex operator ++ (int);
};
Complex Complex::operator + (Complex B)
{
return Complex(real+B.real,image+B.image);
}
Complex Complex::operator - (Complex B)
{
return Complex(real-B.real,image-B.image);
}
Complex Complex::operator - ()
{
return Complex(-real,-image);
}
Complex Complex::operator ++()
{
return Complex(++real,image);
}
Complex Complex::operator ++(int)
{
return Complex(real++,image);
}
int main()
{
Complex A(100.0,200.0),B(-10.0,20.0),C;
cout<<"A = ",A.display();
cout<<"B = ",B.display();
C=A+B;
cout<<"C= A+B = ",C.display();
C=A-B;
cout<<"C= A-B = ",C.display();
C=-A+B;
cout<<"C= -A+B = ",C.display();
C=A++;
cout<<"C= A++ = ",C.display();
C=++A;
cout<<"C= ++A = ",C.display();
C=A+5;
C.display();
return 0;
}
//重载->为类的成员函数
//
#include <iostream>
using namespace std;
class complex
{
private:
double real;
double image;
public:
complex(double real=0.0,double image=0.0)
{
this->real=real;
this->image=image;
}
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
};
class pccomplex
{
private:
complex *pc;
public:
pccomplex(complex * pc =NULL)
{
this->pc=pc;
}
complex * operator->()
{
static complex nullcomplex(0,0);
if(pc==NULL)
{
return &nullcomplex;
}
return pc;
}
};
int main()
{
pccomplex p1;
p1-> display();
complex c1(100,200);
p1=&c1;
p1->display();
return 0;
}
//重载下标运算符[]为类的成员函数的字符串类
//
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char *str;
int len;
public:
void showstr()
{
cout<<"String:"<<str<<",length:"<<len<<endl;
}
String(const char *p=NULL)
{
if(p)
{
len=strlen(p);
str=new char [len+1];
strcpy(str,p);
}
else
{
len=0;
str=NULL;
}
}
~String()
{
if(str!=NULL)
{
delete [] str;
}
}
char &operator[](int n)
{
return *(str+n);
}
const char &operator[](int n)const
{
return *(str+n);
}
};
int main()
{
String s1("0123456789abcdef");
s1.showstr();
s1[10]='A';
cout<<"s1[10]=A"<<endl;
const String s2("ABCDEFGHIJKLMN");
s1.showstr();
cout<<"s2[10]="<<s2[10]<<endl;
return 0;
}
//虚析构函数
//删除动态对象析构函数的调用
#include <iostream>
using namespace std;
class A
{
public:
virtual ~A()
{
cout<<"A::~A() is called."<<endl;
}
A()
{
cout<<"A::A() is called."<<endl;
}
};
class B:public A
{
private:
int *ip;
public:
B(int size=0)
{
ip=new int [size];
cout<<"B::B() is called."<<endl;
}
~B()
{
cout<<"B::~B() is called."<<endl;
delete [] ip;
}
};
int main()
{
A *b=new B(10);
delete b;
return 0;
}
本来有个抽象类的函数的,可是看到书上的cout后面接的是“>>”,立马无语了,然后看了下,还是不大会,有时间看看....
//******************************
// 第十章 类模版与STL
//******************************
书上的程序了还真错了不少....
坑爹的马基课在召唤我......擦
好冷.....