标题:何去何从?
关键词:多态,派生,虚函数太多了
内容
A. 一句话概括今日目标完成情况
(a)10.5、10.6的学习 650%(今天一天把12.8学完了)
(b)没啥
B.具体内容
今天一天没干啥,就学了点C++,本来以为今天能学完,但是会长让我干活,就剩了三节明天学。
感悟就是C++太屌太牛逼了。
员工类定义
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char Name[20];
int Age;
public:
Person(char *name,int age)
{
strcpy(Name,name);
Age=age;
cout<<"constructor of person "<<Name<<endl;
}
~Person(){cout<<"deconstructor of person"<<Name<<endl;}
};
class Employee:public Person
{
private:
char Dept[20];
Person Leader;
public:
Employee(char *name,int age,char *dept,char *name1,int age1):Person(name,age),Leader(name1,age1)
{
strcpy(Dept,dept);
cout<<"constructor of employee "<<endl;
}
~Employee(){cout<<"deconstructor of employee "<<endl;}
};
int main()
{
Employee Andre("Andre",20,"CTO","Luoyonghao",44);
return 0;
}
点,圆,圆柱类
#include<iostream>
#include<cmath>
#include<windows.h>>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int=0,int=0);
void SetPoint(int,int);
int GetX(){return x;};
int GetY(){return y;};
void Print();
};
Point::Point(int a,int b){SetPoint(a,b);}
void Point::SetPoint(int a,int b){x=a,y=b;}
void Point::Print(){cout<<"["<<x<<","<<y<<"]"<<endl;}
class Circle:public Point
{
private:
double radius;
public:
Circle(int x=0,int y=0,double r=0.0);
void SetRadius(double r);
double GetRadius(){return radius;};
double Area();
void Print();
};
Circle::Circle(int a,int b,double r):Point(a,b){SetRadius(r);}
void Circle::SetRadius(double r){radius=(r>0)?r:0;}
double Circle::Area()
{
return 3.14159*radius*radius;
}
void Circle::Print()
{
cout<<"Center=";
Point::Print();
cout<<"; Radius="<<radius<<endl;
}
class Cylinder:public Circle
{
private:
double high;
public:
Cylinder(int x,int y,double r,double h);
void Set_data(int x,int y,double r,double h);
double Area();
double Volume();
void Print();
};
Cylinder::Cylinder(int x,int y,double r,double h):Circle(x,y,r){high=h;}
void Cylinder::Set_data(int x,int y,double r,double h)
{
SetPoint(x,y);
SetRadius(r);
high=h;
}
double Cylinder::Area()
{
double r=GetRadius();
return 2*3.14159*r*r+2*3.14159*r*high;
}
double Cylinder::Volume(){return Circle::Area()*high;}
int main()
{
cout<<"########################## Point Testing ###########################"<<endl;
Point p(12,7);
cout<<endl;
cout<<"The point:";
p.Print();
cout<<endl<<endl;
system("pause");
cout<<"########################## Circle Testing ##########################"<<endl;
Circle c(120,70,15.0);
cout<<"The point:";
c.Point::Print();
cout<<"The area:"<<c.Area();
cout<<endl<<endl;
system("pause");
cout<<"######################### Cylinder Testing #########################"<<endl;
Cylinder C(120,70,15.0,50.0);
cout<<"The point:";
C.Point::Print();
cout<<"The area:"<<C.Circle::Area()<<endl;
cout<<"The volume:"<<C.Area()<<endl;
cout<<endl<<"Thanks for using!";
return 0;
}
mp3,u盘类
#include<iostream>
#include<string.h>
#include<mmsystem.h>
using namespace std;
class UDISK
{
private:
char *crow[100];
int nrow;
public:
UDISK(void){nrow=0;}
void read(void);
void write(char *pstr);
~UDISK(){cout<<"delete UDISK rom"<<endl;}
};
void UDISK::read(void)
{
for(int i=0;i<nrow;i++){cout<<crow[i]<<endl;}
}
void UDISK::write(char *pstr)
{
crow[nrow]=pstr;
nrow++;
}
class MP3:public UDISK
{
public:
MP3():UDISK(){};
void play(char *pstr);
~MP3(){cout<<"delete MP3 rom"<<endl;}
};
void MP3::play(char *pstr)
{
char str[100]="play ";
strcat(str,pstr);
cout<<str;
mciSendStringA(str,NULL,0,NULL);
}
int main()
{
UDISK U;
cout<<"Writing..."<<endl;
U.write("静夜思");
U.write("床前明月光");
U.write("李白睡的香");
U.write("举头望烧鸡");
U.write("低头思烤鸭");
cout<<endl<<"Reading..."<<endl;
U.read();
MP3.M;
cout<<endl<<"Playing..."<<endl;
M.play("D:\CB Files\宋冬野 董小姐");
char a;
cin>>a;
return 0;
}
学生类
#include<iostream>
#include<cstring>
#include<windows.h>
using namespace std;
class Student
{
private:
int stunumber;
char name[20];
int age;
char classname[20];
char schoolname[20];
public:
Student(int stunumber,char *name,int age,char *classname,char *schoolname)
{
this->stunumber=stunumber;
strcpy(this->name,name);
this->age=age;
strcpy(this->classname,classname);
strcpy(this->schoolname,schoolname);
}
void show()
{
cout<<stunumber<<'\t';
cout<<name<<'\t'<<age<<'\t'<<classname<<'\t'<<schoolname<<endl;
}
};
class CollegeStudent:public Student
{
private:
char teachername[20];
public:
CollegeStudent(int stunumber,char *name,int age,char *classname,char *schoolname,
char *teachername):Student(stunumber,name,age,classname,schoolname)
{
strcpy(this->teachername,teachername);
}
void show()
{
Student::show();
cout<<teachername<<endl;
}
};
int main()
{
cout<<"Testing for Student..."<<endl;
int stunumber,age;
char name[20];
char classname[20];
char schoolname[20];
cout<<endl<<"Please input the information of the student:"<<endl;
cin>>stunumber>>name>>age>>classname>>schoolname;
Student s1(stunumber,name,age,classname,schoolname);
cout<<"The information of the student is:"<<endl;
s1.show();
system("pause");
cout<<endl<<"Testing for CollegeStudent..."<<endl;
char teachername[20];
cout<<endl<<"Please input the information of the college student:"<<endl;
cin>>stunumber>>name>>age>>classname>>schoolname>>teachername;
CollegeStudent s2(stunumber,name,age,classname,schoolname,teachername);
cout<<"The information of the college student is:"<<endl;
system("pause");
s2.show();
cout<<endl<<"Thanks for using!"<<endl;
return 0;
}
手机制式类
#include<iostream>
using namespace std;
class mobile
{
public:
mobile(){}
char mynumber[11];
virtual void showinfo(){cout<<"The phone is mobile"<<endl;}
};
class mobilegsm:public mobile
{
public:
mobilegsm(){}
void showinfo(){cout<<"The phone is mobilegsm"<<endl;}
};
class mobilecdma:public mobile
{
public:
mobilecdma(){}
void showinfo(){cout<<"The phone is mobilecdma"<<endl;}
};
int main()
{
mobile m,*p1;
mobilegsm gsm;
mobilecdma cdma;
m=gsm;
m.showinfo();
m=cdma;
m.showinfo();
p1=&gsm;
p1->showinfo();
p1=&cdma;
p1->showinfo();
mobile &p4=gsm;
p4.showinfo();
mobile &p5=cdma;
p5.showinfo();
return 0;
}
圆和矩形求面积周长类
#include<iostream>
#include<cmath>
using namespace std;
#define Pi 3.14159226
class Shape
{
public:
virtual double area()=0;
virtual double cirumenference()=0;
};
class Rectangle:public Shape
{
private:
int x,y;
int width,hight;
public:
Rectangle(int x,int y,int w,int h)
{
this->x=x;this->y=y;
width=w;hight=h;
}
double area(){return width*hight;}
double cirumenference(){return 2.0*(width+hight);}
};
class Circle:public Shape
{
private:
int x,y;
int r;
public:
Circle(int x,int y,int r)
{
this->x=x;this->y=y;this->r=r;
}
double area(){return Pi*r*r;}
double cirumenference(){return 2.0*Pi*r;}
};
int main()
{
Rectangle s1(1,1,1,1);
Circle s2(1,1,1);
Shape *p1=&s1,*p2=&s2;
cout<<"The rectangle:"<<p1->area()<<" "<<p1->cirumenference()<<endl;
cout<<endl<<"The circle:"<<p2->area()<<" "<<p2->cirumenference()<<endl;
cout<<endl<<"Thanks for using!";
return 0;
}
操作符重载类:
#include<iostream>
#include<cmath>
using namespace std;
class Complex
{
private:
double real,imag;
public:
Complex(double r=0,double i=0):real(r),imag(i){}
double Real(){return real;}
double Imag(){return imag;}
Complex operator +(Complex &);
Complex operator +(double);
bool operator ==(Complex);
~Complex(){};
};
Complex Complex::operator + (Complex &c)
{
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
Complex Complex::operator + (double d)
{
Complex temp;
temp.real=real+d;
temp.imag=imag;
return temp;
}
bool Complex::operator == (Complex c)
{
if(real==c.real&&imag==c.imag)
return true;
else
return false;
}
int main()
{
Complex c1=(3,4),c2=(5,6),c3;
cout<<"c1="<<c1.Real()<<"+"<<c1.Imag()<<"i"<<endl;
cout<<"c2="<<c2.Real()<<"+"<<c2.Imag()<<"i"<<endl;
c3=c1+c2;
cout<<"c3="<<c3.Real()<<"+"<<c3.Imag()<<"i"<<endl;
if(c1==c2) cout<<"yes"<<endl;
else cout<<"no"<<endl;
return 0;
}
Fruit
#include<iostream>
using namespace std;
class Fruit
{
public:
virtual void print(){cout<<"Fruit"<<endl;}
};
class Apple:public Fruit
{
void print(){cout<<"Apple"<<endl;}
};
class Banana:public Fruit
{
void print(){cout<<"Banana"<<endl;}
};
class Peach:public Fruit
{
void print(){cout<<"Peach"<<endl;}
};
class Pear:public Fruit
{
void print(){cout<<"Pear"<<endl;}
};
int main()
{
Fruit *pFruit[]={new Fruit(),new Apple(),new Banana(),new Peach(),new Pear()};
for(int i=0;i<5;i++){pFruit[i]->print();}
return 0;
}
riqilei bijiao
#include<iostream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date(int y=1900,int m=1,int d=1){year=y;month=m;day=d;}
void print_ymd();
void print_mdy();
void init(int,int,int);
bool operator > (Date &dd);
~Date(){};
};
void Date::print_ymd(){cout<<year<<"-"<<month<<"-"<<day<<endl;}
void Date::print_mdy(){cout<<month<<"."<<day<<","<<year<<endl;}
void Date::init(int yy,int mm,int dd)
{
year=(yy>=1900&&yy<=2100)?yy:1900;
month=(mm>=1&&mm<=12)?mm:1;
day=(dd>=1&&dd<=31)?dd:1;
}
bool Date::operator > (Date &dd)
{
if(year>dd.year)
return true;
else if(year!=dd.year)
return false;
if(month>dd.month)
return true;
else if(month!=dd.month)
return false;
if(day>dd.day)
return true;
else
return false;
}
int main()
{
Date date1(1996,12,7);
Date date2(1996,12,16);
Date date3(1997,3,7);
Date date4(2007,11,7);
if(date1>date2)cout<<"date1 is bigger than date2"<<endl;
else cout<<"date1 is smaller than date2 or the same with date2"<<endl;
if(date4>date3)cout<<"date4 is bigger than date3"<<endl;
else cout<<"Fuck the whole world!"<<endl;
cout<<endl<<"Thanks for using"<<endl;
return 0;
}
fenshulei
#include<iostream>
using namespace std;
class Fraction
{
private:
int a,b;
int divisor(int p,int q);
public:
Fraction(){a=0;b=1;}
Fraction(int,int);
void set_f(int aa,int bb);
void show_f();
Fraction add(Fraction b);
Fraction operator + (Fraction u);
bool operator == (Fraction u);
Fraction operator - ();
~Fraction(){};
};
Fraction::Fraction(int x,int y){set_f(x,y);}
void Fraction::set_f(int aa,int bb)
{
a=aa;
if(bb!=0) b=bb;
else a=0,b=1;
}
void Fraction::show_f(){cout<<a<<"/"<<b;}
Fraction Fraction::add(Fraction u)
{
int tmp;
Fraction v;
v.a=a*u.b+b*u.a;
v.b=b*u.b;
tmp=divisor(v.a,v.b);
v.a=v.a/tmp;
v.b=v.b/tmp;
return v;
}
Fraction Fraction::operator + (Fraction u)
{
int tmp;
Fraction v;
v.a=a*u.b+b*u.a;
v.b=b*u.b;
tmp=divisor(v.a,v.b);
v.a=v.a/tmp;
v.b=v.b/tmp;
return v;
}
bool Fraction::operator == (Fraction u)
{
float x,y;
x=(float)a/b;
y=(float)u.a/u.b;
if(x==y) return true;
else return false;
}
Fraction Fraction::operator - ()
{
a=(-1)*a;
return *this;
}
int Fraction::divisor(int p,int q)
{
int r;
if(p<q)
{
int tmp;
tmp=q;
q=p;
p=tmp;
}
r=p%q;
while(r!=0)
{
p=q;
q=r;
r=p%q;
}
return q;
}
int main()
{
Fraction f1,f2,f3;
f1.set_f(4,-5);
f2.set_f(3,6);
f1.show_f();
cout<<"+";
f2.show_f();
f3=f1+f2;
cout<<"=";
f3.show_f();
f1.set_f(6,-20);
if(f1==f3) cout<<"\nf1==f3"<<endl;
else cout<<"\nf1!=f3"<<endl;
f2=(-f2);
f2.show_f();
cout<<endl<<"Thanks for using!"<<endl;
return 0;
}
.. -....- .-.. --- ...- . -....- - .... .. ... -....- .-- --- .-. .-.. -..
C.明日计划
把12章结束。