![C 例题:第二部分_第1页](http://file4.renrendoc.com/view12/M09/2A/2E/wKhkGWaNiAmANu6BAADnD5uB9Lg375.jpg)
![C 例题:第二部分_第2页](http://file4.renrendoc.com/view12/M09/2A/2E/wKhkGWaNiAmANu6BAADnD5uB9Lg3752.jpg)
![C 例题:第二部分_第3页](http://file4.renrendoc.com/view12/M09/2A/2E/wKhkGWaNiAmANu6BAADnD5uB9Lg3753.jpg)
![C 例题:第二部分_第4页](http://file4.renrendoc.com/view12/M09/2A/2E/wKhkGWaNiAmANu6BAADnD5uB9Lg3754.jpg)
![C 例题:第二部分_第5页](http://file4.renrendoc.com/view12/M09/2A/2E/wKhkGWaNiAmANu6BAADnD5uB9Lg3755.jpg)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
例5-1
#includeHstdafx.hn
#include"iomanip"
#include"iostream"
usingnamespacestd;
#include"string.h"
#include"student.h"
classStudent
(
private:
char*name;
intstu_no;
floatscore;
staticinttotal;//定义静态数据成员
public:
Student(char*na,intno,floatsco);
voidPrint();
);
intStudent:rtotal=0;〃初始化静态数据成员
intmain()
(
Students1(“张明”,1,90);
sl.Print();
Students2(“王兰”,2,95);
s2.Print();
Students3(“于敏”,3,87);
s3.Print();
return0;
)
Student::Student(char*na,intno,floatsco)
(
name=newchar[strlen(na)+l];
strcpy(name,na);
stu_no=no;
score=sco;
total++;
)
voidStudent::Print()
cout<v"第"<vtotal<<"个学生:"«name«setw(4)«stu_no<<setw(4)«score
«endl;
cout«"总人数是:u«total«endl;
)
第1个学生:张明190
总人数:1
第2个学生:王兰295
总人数:2
第3个学生:于敏387
总人数:3
例5-2
#include"stdafx.h"
#include"iostream"
usingnamespacestd;
include"a.h"
classA
(
private:
staticinta;
intb;
public:
A(inti,intj);
voidshow();
);
intA::a;
intmain()
(
Ax(l,l);
x.show();
Ay(2,2);
y.show();
x.show();
return0;
)
A::A(inti,intj)
(
a=i;
b=j;
}
voidA::show()
cout«nThisisstatica:n«a«endl;
cout«nThisisnon-staticb:n«b«endl;
Thisisstatica:1
Thisisnon-staticb:1
Thisisstatica:2
Thisisnon-staticb:2
Thisisstatica:2
Thisisnon-staticb:1
例5-3
#include"stdafx.h'1
#include"iomanip"//employee.cpp
#include"iostream"
usingnamespacestd;
#include"employee.h"
classEmployee{//employee.h
private:
char*name;
intnumber;
staticinttotal;
public:
Employee();
staticvoidPrint();
voidPrintlnfo();
);
intEmployee::total=0;
intmain()
(
Employee::Print();//在未定义对象之前就可以通过类名访问静态成员
函数
Employees[3];
inti;
cout«endl;
for(i=0;i<3;i++)
s[i].Printinfo();
Employee::Print();
return0;
Employee::Employee()
{
name二newchar[10];
cout<<"输入职工姓名和编号"<<endl;
cin»name»number;
total++;
voidEmployee::Print()
(
coutvvendkv”总人数:n«total«endl;
}
voidEmployee::PrintInfd()
(
cout<<"姓名:"v<name«setw(7)vv”编号:n«number«endl;
)
总人数:0
输入职工姓名和编号
sun1
输入职工姓名和编号
wang2
输入职工姓名和编号
li3
姓名:sun编号:1
姓名:wang编号:2
姓名:li编号:3
总人数:3
例5-4
#include"stdafx.h'1
#include"iomanip'*//employee.cpp
#include"iostream"
usingnamespacestd;
#include"employee.h"
classEmployee{//employee.h
private:
char*name;
intnumber;
staticinttotal;
public:
Employee();
staticvoidPrint(Employee);
);
intEmployee::total=0;
intmain()
Employees;
Employee::Print(s);
return0;
)
Employee::Employee()
(
name=newchar[10];
cout«"输入职工姓名和编号"<<endl;
cin»name»number;
total++;
)
voidEmployee::Print(Employeea)
(
coutvVendlvv”姓名:”《<<setw(7)v<”编号:M«a.number«endl;
cout«”总人数:H«total«endl;
)
输入职工姓名和编号
sun1
姓名:sun编号:1
总人数:1
例5-5
#include"stdafx.h',
#include"iostream"//static.cpp
usingnamespacestd;
#include"obj.h"
classObj//obj.h
(
private:
charch;
public:
Obj(charc);
~Obj();
);
voidf();
voidg();
ObjA('A');〃定义全局对象A
intmain()
(
cout«ninsidemain()n«endl;
f();
f();
g();
cout«noutsidemain()u«endl;
return0;
)
Obj::Obj(charc):ch(c)
(
cout«Mconstruct.......n«ch«endl;
)
Obj:-Obj()
(
cout«ndestruct."«ch«endl;
)
voidf()
(
staticObjB(B);
)
voidg()
(
staticObjC(C);
)
construct......A
insidemain()
construct......B
construct......C
outsidemain()
destruct.......C
destruct.......B
例5-6
#include"stdafx.h"
#include"iostream"//test.cpp
usingnamespacestd;
#includentest.h"
classTest{//test.h
private:
inta;
public:
voidfl();
voidf2();
intmain()
Testtl;
tl.f2();
tl.flO;
return0;
)
voidTest::fl()
(
f2();〃调用类中的成员函数f2
cout«a«endl;〃使用类中的成员变量a
}
voidTest::f2()
(
a=100;
100
例5-7
#include"stdafx.h"
#include"iostream"//obj.cpp
usingnamespacestd;
#include"obj.h"
classObj//obj.h
(
private:
charch;
public:
Obj(charc);
〜Obj();
);
voidf();
ObjA('A');〃定义全局对象A
intmain()
cout«Minsidemain()n«endl;
f();
f();
cout«Houtsidemain()n«endl;
return0;
)
Obj::Obj(charc):ch(c)
(
cout«nconstruct.n«ch«endl;
Obj::~Obj()
cout«"destruct........"«ch«endl;
)
voidf()
(
staticObjB('B');
ObjC(C。
)
construct........A
insidemain()
construct........B
construct........C
destruct........C
construct........C
destruct.......C
outsidemain()
destruct.......B
例5-8
#include"stdafx.h"
#include"iostream'1//test.cpp
#include"string"
usingnamespacestd;
#include"test.hn
namespaceA//test.h
charuser_name[]=HnamespaceA”;
voidshowname()
(
cout«user_name«endl;
)
)
namespaceB
(
stringuser_name="namespaceB”;
voidshowname()
cout«user_name«endl;
intmain()
(
A::showname();〃用命名空间限制符访问函数showname()
B::showname();〃用命名空间限制符访问函数showname()
strcpy(A::user_name,”good");〃用命名空间限制符访问变量user_name
A::showname();
return0;
)
namespaceA
namespaceB
good
例6-1
#include"stdafx.h"
#includeniomanipn//point.cpp
#include"iostream"
usingnamespacestd;
#include'^string.h^^
#include"student.h”
classPoint//point.h
(
private:
intx,y;
public:
Point(inti=0,intj=0);
Pointoperator-();
voidprint();
);
intmain()
(
Pointobi(1,2);
cout«nob1:"«endl;
obl.print();
cout«n-ob1:M«endl;
ob1.operator-();〃显示调用
//ob1=-ob1;//隐式调用
obl.print();
return0;
Point::Point(inti,intj)
x=i;
y=j;
)
voidPoint::print()
(
cout«l,(x,y)n«setw(5)«,,(n«x«",',«y«n),,«endl;
}
PointPoint::operator-()
x=-x;
y=-y;
return*this;
obi:
(x,y)(1,2)
-obi:
(x,y)
例6-2
#includeMstdafx.h"
#include"iostream"//str.cpp
usingnamespacestd;
#includenstring.hn
#include"str.h"
constintMAXSIZE=20;//str.h:存放字符串的最大长
度
classString
(
private:
charbufferfMAXSIZE];
intlength;
public:
String(char*in_str);
StringO;
Stringoperator+(char*append_str);〃重载"+”运算符
voidShowStringO;
);
intmain()
(
Stringtitle(nC/C++”);
title=title+"Program”;〃隐式调用
//title=title.operator+(nProgramn);〃显式调用
title.ShowStringO;
return0;
)
String::String(char*in_str)
(
strcpy(buffer,in_str);
length=strlen(buffer);
)
String::String()
(
length=0;
)
StringString::operator+(char*append_str)
(
Stringtemp;
inttemplen;
templen=strlen(buffer)+strlen(append_str)+1;〃计算相加后的字符串的总长度
if(templen>MAXSIZE)
(
cout«nStringistoolarge!n«endl;
strcpy(temp.buffer,buffer);
returntemp;
)
length=templen;
strcpy(temp.buffer,buffer);
strcat(temp.buffer,append_str);〃字符串相力口
returntemp;
)
voidString::ShowString()
(
cout«buffer«endl;
)
C/C++Program
例6-3
#include"stdafx.h"
#include"iostream'*//complex.cpp
usingnamespacestd;
#include"complex.h"
classComplex//complex.h
private:
doublereal;
doubleimag;
public:
Complex(doubler=0.0,doublei=0.0);
voidprint();
Complexoperator+(Complexa);〃重载加法运算符
Complexoperator-(Complexa);〃重载减法运算符
);
intmain()
(
Complexcom1(1.1,2.2),com2(3.3,4.4),total;
total=coml+com2;〃隐式调用
total.print();
total=coml-com2;〃隐式调用
total.print();
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
)
ComplexComplex::operator+(Complexa)
(
Complextemp;
temp.real=real+a.real;
temp.imag=imag+a.imag;
returntemp;
)
ComplexComplex::operator-(Complexa)
(
Complextemp;
temp.real=real-a.real;
temp.imag=imag-a.imag;
returntemp;
}
voidComplex::print()
(
cout«real;
if(imag>0)
cout«n+n;
if(imag!=0)
cout«imag«,,i,,«endl;
4.4+6.6i
-2.2-2.2i
例6-4
#include"stdafx.h',
#include"iostream"//point.cpp
usingnamespacestd;
#include"point.h"
classPoint//point.h
(
private:
intx,y;
public:
Point(inti=0,intj=0);
Pointoperator++();
Pointoperator-();
Pointoperator++(int);
Pointoperator-(int);
voidprint();
);
intmain()
(
Pointob1(1,2),ob2(4,5),ob;
cout«nobl:n;
obl.print();
cout«n++obl:n;
++obl;〃前缀++运算符重载
cout«nobl:n;
obl.printO;
cout«nob2:n;
ob2.print();
cout«n—ob2:
-ob2;〃前缀--运算符重载
cout«nob2:";
ob2.print();
cout«nob=ob1++n«endl;
ob=obl++;〃后缀++运算符重载
cout«nob:";
ob.print();
cout«nobl:";
obl.printO;
cout«nob=obl—"«endl;
ob=ob1-;〃后缀--运算符重载
cout«nob:";
ob.print();
cout«nobl:";
obl.print();
return0;
)
Point::Point(inti,intj)
(
x=i;
y=j;
)
voidPoint::print()
(
cout«"("«x«","«y«")"«endl;
}
PointPoint::operator++()
(
++x;
++y;
return*this;
)
PointPoint::operator-()
(
-x;
-y;
return*this;
)
PointPoint::operator++(int)
(
Pointtemp=*this;//保存原对象值
x++;
y++;
returntemp;
)
PointPoint::operator-(int)
(
Pointtemp=*this;//保存原对象值
x-;
y-;
returntemp;
ob1:(1,2)
++obl:obl:(2,3)
ob2:(4,5)
-ob2:ob2:(3,4)
ob=obl++
ob:(2,3)
obi:(3,4)
ob=obl—
ob:(3,4)
obi:(2,3)
例6-5
#include"stdafx.h"
#include"iostream”//student.cpp
#include"iomanip"
usingnamespacestd;
#include"student.h"
classStudent//student.h
(
private:
char*name;
intscore;
public:
Student(char*na,ints);
〜Student。;
voidprint();
);
intmain()
(
Studentpl("zhang”,90);
Studentp2("wang",80);
p2=pl;
cout«np2:n;
p2.print();
return0;
)
Student::Student(char*na,ints)
name=newchar[strlen(na)+l];
strcpy_s(name,strlen(na)+l,na);
score=s;
Student::-Student()
(
deletename;
)
voidStudent::print()
(
cout«name«setw(5)«score«endl;
)
此程序有问题问大神答案思密达〜
例6-6
#include"stdafx.h"
#include"iostream”//str.cpp
usingnamespacestd;
#include"str.h"
const血MAXSIZE=20;//str.h,存放字符串的最大长
度
classString
(
private:
char"buffer;
intlength;
public:
String(char*in_str);
constString&operator+=(constString&);〃声明+二运算符重载函数
voidShowStringO;
〜String。;
);
intmain()
(
Stringsi("happyn),s2("birthday'1);
sl+=s2;
sl.ShowStringO;
sl+-ftoyou”;
sl.ShowStringO;
return0;
)
String::String(char*in_str)
length=strlen(in_str);
buffer=newchar[length+l];
strcpy(buffer,in_str);
)
constString&String::operator+=(constString&append_str)
//定义+=运算符重载函数
char*temp二buffer;〃指向原字符串所占空间
length+=append_str.length;〃计算连接后的字符串的长度
buffer=newchar[length+l];//重新分配空间
strcpy(buffer,temp);〃将原字符串复制到分配的空间中
strcat(buffer,append_str.buffer);//^append_str的字符串连接到所分配的空间
中
deletef]temp;〃释放原字符串所占空间
return*this;
voidString::ShowString()
(
cout«buffer«endl;
)
String::-String()
(
delete[]buffer;
)
happybirthday
happybirthdaytoyou
例6-7
#include"stdafx.h"
#include"iostream”//vector.cpp
#include"iomanip"
usingnamespacestd;
#include"vector.h"
classVector//vector.h
(
private:
intv[3];
public:
Vector(intal,inta2,inta3);
int&operator[](intbi);
intmain()
inti;
Vectorv(l,3,5);
coutv<”修改前:n«endl;
for(i=0;i<3;i++)
cout«v[i]«setw(4);
cout«endl;
cout<<"修改后:H«endl;
for(i=0;i<3;i++)
v[i]=2*i;
for(i=0;i<3;i++)
cout«v[i]«setw(4);
cout«endl;
return0;
)
Vector::Vector(intal,inta2,inta3)
(
v[0]=al;
v[l]=a2;
v[2]=a3;
)
int&Vector::operator[](intbi)
(
if(bi<0||bi>=3)
(
cout«nBadsubscript!u«endl;
exit(l);
)
returnv[bi];
修改前:
135
修改后:
024
例6-8
#include"stdafx.h"
#include"iostream"
usingnamespacestd;
#include"matrix.h"
classMatrix//matrix.h
private:
int*m;
introw,col;
public:
Matrix(int,int);
int&operator()(int,int);
);
intmain()
(
MatrixaM(10,10);
cout«aM(3,4)<<endl;
aM(3,4)=35;
cout«aM(3,4)«endl;
return0;
)
Matrix::Matrix(intr,intc)
(
row=r;
col=c;
m=newint[row*col];
for(inti=0;i<row*col;i++)
*(m+i)=i;
)
int&Matrix::operator()(intr,intc)
(
return(*(m+r*col+c));
)
34
35
例6-9
#include"iostream"//Complex.cpp
usingnamespacestd;
#include"complex.h"
classComplex;
Complexoperator+(Complexa,Complexb);
Complexoperator-(Complexa,Complexb);
classComplex//Complex.h
(
private:
doublereal;
doubleimag;
public:
Complex(doubler=0.0,doublei=0.0);
voidprint();
friendComplexoperator+(Complexa,Complexb);
friendComplexoperator-(Complexa,Complexb);
);
intmain()
(
Complexcom1(5.5,6.6),com2(7.7,9.8),total;
total=coml+com2;
total.print();
total=coml-com2;
total.print();
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
)
Complexoperator+(Complexa,Complexb)
(
Complextemp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
returntemp;
)
Complexoperator-(Complexa,Complexb)
{
Complextemp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
returntemp;
)
voidComplex::print()
(
cout«real;
if(imag>0)
cout<<"+";
if(imag!=0)
cout«imag«Hin«endl;
13.2+16.4i
-2.2-3.2i
例6-10
#includeMstdafx.h"
#include"iostream"//point.cpp
#include"iomanip'*
usingnamespacestd;
#include"point.h"
classPoint;
Pointoperator-(Pointob);
classPoint//point.h
(
private:
intx,y;
public:
Point(inti=0,intj=0);
friendPointoperator-(Pointob);
voidprint();
);
intmain()
(
Pointobl(l,2),ob2;
cout«nob1:"«endl;
obl.print();
cout«n-obl:u«endl;
ob2=-obl;
ob2.print();
return0;
)
Point::Point(inti,intj)
(
x=i;
y=j;
}
voidPoint::print()
(
cout«"(x,y)"«setw(5)«"("«x«","«y«")"«endl;
)
Pointoperator-(Pointob)
ob.x=-ob.x;
ob.y-ob.y;
returnob;
}
obi:
(x,y)(1,2)
-obi:
(x,y)(-1,-2)
例6-11
#include"stdafx.h"
#include"iostream"//point.cpp
#include"iomanip"
usingnamespacestd;
#include"point.h'1
classPoint//point.h
(
private:
intx,y;
public:
Point(inti=0,intj=0);
friendPointoperator++(Pointob);
voidprint();
);
intmain()
(
Pointobi(1,2);
cout«nob1:n«endl;
obl.print();
cout«n++obl:"«endl;
++obl;
obl.print();
return0;
)
Point::Point(inti,intj)
(
x=i;
y=j;
)
voidPoint::print()
cout«"(x,y)"«setw(5)«"("«x«","«y<<")"«endl;
Pointoperator++(Pointob)
(
++ob.x;
++ob.y;
returnob;
}
obi:
(x,y)(1,2)
++ob1:
(x,y)(1,2)
例6-12
#include"stdafx.h"
#include"iostream”//point.cpp
#include"iomanip"
usingnamespacestd;
#include"point.h"
classPoint//point.h
(
private:
intx,y;
public:
Point(inti=0,intj=0);
friendPointoperator++(Point&ob);
voidprint();
);
intmain()
(
Pointobi(1,2);
cout«nob1:n«endl;
obl.print();
cout«M+4-ob1:u«endl;
++obl;
obl.printO;
return0;
}
Point::Point(inti,intj)
(
x=i;
y=j;
voidPoint::print()
(
cout«"(x,y)"«setw(5)«"("«x<<","«y«")"«endl;
)
Pointoperator++(Point&ob)
(
++ob.x;
++ob.y;
returnob;
)
obi:
(x,y)(1,2)
++obl:
(x,y)(2,3)
例6-13
#includeHstdafx.hn
#include"iostream"//Complex.cpp
#include"iomanip”
usingnamespacestd;
#include"Complex.h"
classComplex;
Complexoperator+(Complexc,inta);
Complexoperator+(inta,Complexc);
classComplex//Complex.h
(
private:
doublereal;
doubleimag;
public:
Complex(doubler=0.0,doublei=0.0);
voidprint();
friendComplexoperator+(Complexc,inta);
friendComplexoperator+(inta,Complexc);
);
intmain()
(
Complexcom(l.l,2.2);
com=com+10;
cout«ncom+10H«endl;
com.print();
cout«n10+comH«endl;
com=10+com;
com.print();
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
)
Complexoperator+(Complexc,inta)
(
Complextemp;
temp.real=c.real+a;
temp.imag=c.imag+a;
returntemp;
)
Complexoperator+(inta,Complexc)
(
Complextemp;
temp.real=a+c.real;
temp.imag=a+c.imag;
returntemp;
)
voidComplex::print()
(
cout«real;
if(imag>0)
cout«n+";
if(imag!=0)
cout«imag«nin<<endl;
)
com+10
ll.l+12.2i
10+com
21.1+22.2i
例6-14
#include"stdafx.h"
#include"iostream'1
usingnamespacestd;
#include'^string.h^^
#include"str.hn
classString//str.h
(
private:
char*str;
intlength;
public:
String(char*s);
voidPrint();
〜String。;
};
intmain()
(
Strings=”C/C++program1';
s.Print();
return0;
)
String::String(char*s)
(
length=strlen(s);
str=newchar[length+l];
strcpy(str,s);
}
voidString::Print()
(
cout«str«endl;
)
String::-String()
(
delete[]str;
)
C/C++program
例6-15
#include"stdafx.h"
#include"iostream”//Complex.cpp
#include"iomanip'1
usingnamespacestd;
#include"complex.h"
classComplex;
Complexoperator+(Complexcl,Complexc2);
classComplex//Complex.h
private:
doublereal;
doubleimag;
public:
Complex();
Complex(intr);
Complex(doubler,doublei);
voidprint();
friendComplexoperator+(Complexc1,Complexc2);
};
intmain()
(
Complexcom1(1.1,2.2),com2;
Complexcom3=10;
cout«ncoml:";
coml.print();
coml=20+coml;
cout«ncom1=20+com1:";
coml.print();
com2=10+200;
cout«f,com2=10+200:”;
com2.print();
cout«endl;
cout«ncom3=10:";
com3.print();
cout«endl;
return0;
)
Complex::Complex()
(
real=0;
imag=0;
)
Complex::Complex(intr)
(
real=r;
imag=0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
Complexoperator+(Complexcl,Complexc2)
(
Complextemp;
temp.real=cl.real+c2.real;
temp.imag=c1.imag+c2.imag;
returntemp;
}
voidComplex::print()
(
cout«real;
if(imag>0)
cout«n+";
if(imag!=0)
cout«imag«nin<<endl;
)
coml:l.l+2.2i
com1=20+com1:21.1+2.2i
com2=10+200:210
com3=10:10
例6-16
#include"stdafx.h"
#include"iostream”//complex.cpp
usingnamespacestd;
#include"complex.hn
classComplex//complex.h
(
private:
doublereal;
doubleimag;
public:
Complex(doubler=0,doublei=0);
operatorfloat();
operatorint();
voidPrint();
);
intmain()
(
Complexa(2.2,4.4);
a.Print();
cout«float(a)*0.5«endl;
Complexb(4.7,6);
b.Print();
cout«int(b)*2«endl;
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
cout«uConstructing....n«endl;
)
Complex::operatorfloat()
(
cout«nTypechangedtofloat...."«endl;
returnreal;
)
Complex:operatorint()
(
cout«nTypechangedtoint....M«endl;
returnint(real);
}
voidComplex::Print()
(
cout«,(,«real«V«imag«,),«endl;
)
Constructing....
(2.2,4.4)
Typechangedtofloat....
1.1
Constructing....
(4.7,6)
Typechangedtoint....
8
例6-17
#includeMstdafx.h"
#include"iostream"//complex.cpp
usingnamespacestd;
#include"complex.hn
classComplex//complex.h
private:
doublereal;
doubleimag;
public:
Complex(doubler,doublei);
Complex(doublei=0);
operatordouble();
voidPrint();
);
intmain()
(
Complexcom1(1.1,2.2),com2(2.3,3.2),com;
com=com1+com2;
com.Print();
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
)
Complex::Complex(doublei)
(
real=imag=i;
)
Complex::operatordouble()
(
cout«nTypechangedtodouble....n«endl;
returnreal+imag;
)
voidComplex::Print()
(
cout«,(,«real«,/«imag«,),«endl;
)
Typechangedtodouble....
Typechangedtodouble....
(8.8,8.8)
例6-18
#include"stdafx.h',
#include"iostream"//complex.cpp
usingnamespacestd;
#include"complex.hn
classVector{//complex.h
private:
doublex,y;
public:
Vector(doubletx=0,doublety=O);
voidprint();
);
classComplex
(
private:
doublereal;
doubleimag;
public:
Complex(doubler=0,doublei=0);
operatorVector();
);
intmain()
(
Complexcom(l.l,2.2);
Vectorvec;
vec=com;
vec.printO;
return0;
)
Complex::Complex(doubler,doublei)
(
real=r;
imag=i;
)
Complex::operatorVector()
(
returnVector(real,imag);
)
Vector::Vector(doubletx,doublety)
(
x=tx;
y=ty;
}
voidVector::print()
(
cout«"("«x«","«y«")"«endl;
(1.1,2.2)
例7-1
#include"myclass.h"
#ifndefSTUDENT_H_
#defineSTUDENT_H_
classStudent{
intNo;
charName[20];
public:
Student();
intGetNo()const;
constchar*GetName()const;
);
#endif
#ifndefMYCLASS_H_
#defineMYCLASS_H_
#include"student.h"
classMyclass{
enum{NUM=50};
charcName[20];〃班级名称
intiNum;//班级人数
StudentstuList[NUM];〃学生列表
public:
MyclassQ;
constchar*GetClassName();
constchar*GetStuName(intiNo);〃通过学号得到某个学生的姓名
);
#endif
intmain(intargc,char*argv[])
(
Myclassmyclass;
return0;
)
此程序有问题,请找大神思密达〜
例7-2
#include<iostream>
usingnamespacestd;
classPoint{//基类Point类的声明
private:
floatX,Y;
public:
voidInitP(floatxx=0,floatyy=0){
X二xx;Y=yy;
)
voidMove(floatxOff,floatyOff){〃点的移动
X+=xOff;Y+=yOff;
)
floatGetX(){returnX;}
floatGetY(){returnY;}
);
classRectangle:publicPoint{//派生类声明
private:〃新增私有数据成员
floatW,H;〃矩形的宽、高
public:〃新增公有函数成员
voidInitR(floatx,floaty,floatw,floath){
InitP(x,y);〃调用基类公有成员函数
W=w;
H=h;
)
floatGetH(){returnH;}
floatGetW(){returnW;}
);
intmain(){
Rectanglerect;
rect.InitR(2,3,20,10);〃通过派生类对象访问基类公有成员
//rect.H;//错误,父类私有成员
//rect.W;〃错误,父类私有成员
rect.Move(3,2);
cout«rect.GetX()«\,
«rect.GetY()«\,
«rect.GetH()«,/
«rect.GetW()«endl;
return0;
)
5,5,10,20
例7-3
#include"stdafx.h',
#ifndefPERSON_H_
#definePERSON_H_
classPerson{〃定义人类
charstrName[20];〃姓名
intiAge;//年龄
charcSex;〃性别
public:
Person(constchar*cpName=NULL,intage=O,charsex='m');
constchar*GetName(){returnstrName;}
intGetAge(){returniAge;}
charGetSex(){returncSex;}
voidSetName(constchar*cpName);
voidSetAge(intage){iAge=age;}
voidSetSex(charsex){cSex=sex;}
);
classTeacher:publicPerson{
intiWID;//工号
public:
Teacher(constchar*cpName=NULL,intage=0,charsex=,m,,int
no=0):Person(cpName,age,sex),iWID(no){
}
intGetWID(){returniWID;}
voidSetWID(intno){iWID=no;}
);
classStudent:publicPerson{〃学生类
intiNo;//学号
floatfScore[5];//5门课程的成绩
floatfAve;〃平均成绩
public:
Student(constchar*cpName=NULL,intage=0,charsex='m;int
no=0):Person(cpName,age,sex),iNo(no){
}一
intGetNo(){returniNo;}
voidSetNo(intno){iNo=no;}
voidSetScore(constfloatfData[]);
floatGetAveScore(){returnfAve;}〃得到平均成绩
);
#endif
#ifndefMYCLASS_H_
#defineMYCLASS_H_
#include"person.hn
classMyClass{
enum{NUM=50};
StudentstuListfNUM];〃学生列表
intiNum;//人数
Teachertea;〃辅导员
charstrClassName[20];//班级名称
public:
MyClass(){iNum=O;}
};
#endif
int_tmain(intargc,_TCHAR*argvf])
(
return0;
)
无结果
例7-4
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
classA{
inta;
public:
A(inti=0):a(i){
cout«"AisconstructedH«endl;
);
~A(){
cout«HAisdestructed"«endl;
)
);
classB:publicA{
intb;
public:
B(intj=0):b(j){
cout«"Bisconstructedn«endl;
)
〜B(){
cout«"Bisdestructedn«endl;
)
);
int_tmain(intargc,_TCHAR*argvf])
(
Bb;
return0;
Aisconstructed
Bisconstructed
Bisdestructed
Aisdestructed
例7-5
#include"stdafx.h"
#include<iostream>
usingnamespacestd;
classX{
public:
X()(
cout«nXisconstructed"«endl;
〜X(){
cout«nXisdestructedn«endl;
classA{
inta;
Xx;
public:
A(inti=0):a(i){
cout«nAisconstructedH«endl;
~A(){
cout«nAisdestructedn«endl;
)
);
classY{
inty;
public:
Y(inti=0){
y=i;
cout«nYisconstructedH«endl;
〜Y(){
cout«nYisdestructedH«endl;
);
classZ{
intz;
public:
Z(inti=0){
z=i;
cout«nZisconstructedH«endl;
〜Z(){
cout«nZisdestructedn«endl;
)
);
classB:publicA{
intb;
Yy;
Zz;
public:
B(intj=O):A(l),z0),b0),yC){
cout«nBisconstructedH«endl;
)
~B(){
cout«"Bisdestructedn«endl;
)
);
血_tmain(intargc,_TCHAR*argv[])
(
Bb;
return0;
)
Xisconstructed
Aisconstructed
Yisconstructed
Zisconstructed
Bisconstructed
Bisdestructed
Zisdestructed
Yisdestructed
Aisdestructed
Xisdestructed
例7-6
#include"sldafx.h"
#include<iostream>
usingnamespacestd;
classA{
inta;
public:
A(){
cout«"Aisconstructed"«endl;
);
~A(){
cout«"Aisdestructed"«endl;
)
);
classY{
血y;
public:
Y(inti=0){
y=i;
cout«HYisconstructedn«endl;
)
〜Y(){
cout«"Yisdestructed"«endl;
)
);
classB:publicA{
intb;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 苏科版数学八年级上册5.1《物体位置的确定》听评课记录
- 八年级道德与法治下册第三单元人民当家作主第五课我国基本制度第3框基本政治制度(第1课时中国共产党领导的多党合作和政治协商制度)听课评课记录(新人教版)
- 人教版九年级数学上册第二十五章概率初步《25.3用频率估计概率》听评课记录
- 八年级思想读本《6.2军强才能国安》听课评课记录
- 小学二年级上乘法口算天天练
- 五年级下册数学听评课记录《折纸》北师大版
- 孵化楼租赁合同范本
- 二零二五年度酒店设施租赁及使用权购买合同
- 外架工班组劳务分包协议书范本
- 工程项目全过程管理协议书范本
- 一级建造师继续教育最全题库及答案(新)
- 2022年高考湖南卷生物试题(含答案解析)
- GB/T 20909-2007钢门窗
- GB/T 17854-1999埋弧焊用不锈钢焊丝和焊剂
- GB/T 15593-2020输血(液)器具用聚氯乙烯塑料
- 直线加速器专项施工方案
- 联苯二氯苄生产工艺及产排污分析
- 储能设备项目采购供应质量管理方案
- 2022年全国卷高考语文答题卡格式
- 复旦大学简介 (课堂PPT)
- CKD马达使用说明
评论
0/150
提交评论