




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
例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"http://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"http://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"http://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"http://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"http://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"http://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"http://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"http://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"http://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"http://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"http://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"http://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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
評論
0/150
提交評論