C++例题第一部分.docx
《C++例题第一部分.docx》由会员分享,可在线阅读,更多相关《C++例题第一部分.docx(61页珍藏版)》请在冰豆网上搜索。
![C++例题第一部分.docx](https://file1.bdocx.com/fileroot1/2023-4/21/82672efc-6472-47e6-a283-00d373e5b01e/82672efc-6472-47e6-a283-00d373e5b01e1.gif)
C++例题第一部分
//2_4.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iostream"//student.cpp
usingnamespacestd;
#defineN5//定义常量N表示有N个学生//student.h
structStudent//声明结构体类型,用来表示学生
{
longlNum;//学号
charcName[12];//姓名
floatfGrade;//成绩
};
classOurClass//声明一个班级类
{
private:
Students[N];//定义N个学生
public:
voidInput();//输入学生信息
voidPrint();//输出学生信息
voidSort();//按学生成绩进行排序
};
intmain()
{
OurClasscl;//定义对象cl
cl.Input();//输入N个学生的信息:
学号、姓名、成绩
cl.Sort();//按学生成绩进行排序
cout<"<cl.Print();//输出N个学生的信息:
学号、姓名、成绩
return0;
}
voidOurClass:
:
Input()
{
inti;
cout<<"输入"<for(i=0;icin>>s[i].lNum>>s[i].cName>>s[i].fGrade;
}
voidOurClass:
:
Sort()
{
inti,j;
StudentTemp;
for(i=0;i{
for(j=i+1;j{
if(s[i].fGrade
{
//交换学生信息
Temp=s[i];
s[i]=s[j];
s[j]=Temp;
}
}
}
}
voidOurClass:
:
Print()
{
inti;
cout<<"学号姓名成绩"<for(i=0;icout<
}
//2_5.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iomanip"//point.cpp
#include"iostream"
usingnamespacestd;
classPoint{//point.h
private:
intx,y;
public:
voidSetPoint(inta,intb);
voidPrint();
};
intmain()
{
Pointp1,p2;//定义对象p1、p2
p1.SetPoint(3,5);
cout<<"p1:
"<p1.Print();
cout<<"p2:
"<p2.Print();
p2=p1;//对象赋值
cout<<"p2=p1:
"<p2.Print();
return0;
}
voidPoint:
:
SetPoint(inta,intb)
{
x=a;
y=b;
}
voidPoint:
:
Print()
{
cout<<"x="<}
答案:
p1:
X=3y=5
P2:
X=-858993460y=-858993460
P2=p1:
X=3y=5
//2_6.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iostream"//date.cpp
usingnamespacestd;
#include"date.h"
classDate{//date.h
private:
intyear,month,day;
public:
Date(inty,intm,intd);
voidPrint();
};
intmain()
{
Datetoday(2010,3,1);
cout<<"todayis";
today.Print();
return0;
}
Date:
:
Date(inty,intm,intd)
{
year=y;
month=m;
day=d;
}
voidDate:
:
Print()
{
cout<}
答案:
todayis2010_3_1
//2_7.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iostream"//point.cpp
usingnamespacestd;
#include"point.h"
classPoint{//point.h
private:
intx,y;
public:
Point(inta,intb);
~Point();
};
intmain()
{
Pointp1(1,2),p2(3,5);
return0;
}
Point:
:
Point(inta,intb)//定义构造函数
{
cout<<"constructor......."<x=a;
y=b;
cout<<'('<}
Point:
:
~Point()//定义析构函数
{
cout<<"destructor........"<cout<<'('<}
答案:
constrctor……..
(1,2)
constrctor……..
(3,5)
destructor.......
(3,5)
destructor.......
(1,2)
//2_8.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"
usingnamespacestd;
#include"complex.h"
voidfun(Complexc);
classComplex{//complex.h
private:
doublereal,imag;
public:
Complex(doubler,doublei);
~Complex();
};
intmain()
{
cout<<"insidemain"<Complexc1(1.1,2.2);
fun(c1);
cout<<"outsidemain"<return0;
}
Complex:
:
Complex(doubler,doublei)
{
cout<<"constructor......."<real=r;
imag=i;
}
Complex:
:
~Complex()
{
cout<<"destructor........"<}
voidfun(Complexc)
{
cout<<"insidefun"<}
Daan:
insidemain
constructor…..
Insidefun
destructor…….
outsidemain
destructor……
//2_10.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//point.cpp
usingnamespacestd;
#include"point.h"
classPoint//point.h
{
private:
intx,y;
public:
Point(inta,intb);
~Point();
};
intmain()
{
Point*p=newPoint(1,3);//动态创建对象,自动调用构造函数
deletep;//删除对象,自动调用析构函数
return0;
}
Point:
:
Point(inta,intb)
{
cout<<"constructor....."<x=a;
y=b;
}
Point:
:
~Point()
{
cout<<"destructor......"<}
Daan:
constructor….
Destructor…..
//2_11.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//circle.cpp
usingnamespacestd;
#include"circle.h"
classCircle//circle.h
{
private:
doubleradius;
public:
Circle(doubler);
doubleArea();
~Circle();
};
intmain()
{
Circlec[3]={1,3,5};
inti;
for(i=0;i<3;i++)
cout<return0;
}
Circle:
:
Circle(doubler)
{
cout<<"construct.........."<radius=r;
}
doubleCircle:
:
Area()
{
return3.14*radius*radius;
}
Circle:
:
~Circle()
{
cout<<"destruct..........."<}
Daan:
constructor….
Destructor……
//2_12.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//point.cpp
usingnamespacestd;
#include"point.h"
classPoint{//point.h
private:
intx,y;
public:
Point(inta,intb);
voidPrint();
};intmain()
{
Pointob[3]={Point(1,2),Point(3,4),Point(5,6)};
inti;
for(i=0;i<3;i++)
ob[i].Print();
return0;
}
Point:
:
Point(inta,intb)
{
x=a;
y=b;
}
voidPoint:
:
Print()
{
cout<<'('<}
Daan:
(1,2)
(3,4)
(5,6)
//2_13.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//circle.cpp
usingnamespacestd;
#include"circle.h"
classCircle//circle.h
{
private:
doubleradius;
public:
Circle(doubler);
doubleArea();
~Circle();
};
intmain()
{
Circle*c=newCircle(3);
cout<Area()<deletec;
return0;
}
Circle:
:
Circle(doubler)
{
cout<<"construct.........."<radius=r;
}
doubleCircle:
:
Area()
{
return3.14*radius*radius;
}
Circle:
:
~Circle()
{
cout<<"destruct..........."<}
Daan:
construct…..
28.26
destruct……
//2_14.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//circle.cpp
usingnamespacestd;
#include"circle.h"
classCircle//circle.h
{
private:
doubleradius;
public:
Circle(doubler);
doubleArea();
~Circle();
};
intmain()
{
Circlec[3]={1,3,5};
Circle*p=c;
for(;pcout<Area()<return0;
}
Circle:
:
Circle(doubler)
{
cout<<"construct.........."<radius=r;
}
doubleCircle:
:
Area()
{
return3.14*radius*radius;
}
Circle:
:
~Circle()
{
cout<<"destruct..........."<}
Daan:
constructor…..
constructor…..
constructor…..
3.14
25.26
78.5
Destructor…….
Destructor…….
Destructor…….
//2_15.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//square.cpp
usingnamespacestd;
#include"square.h"
classSquare//square.h
{
private:
doublelength;
public:
Square(doublel);
doubleArea();
};
intmain()
{
Squares1(3),s2(5);
cout<<"s1areais"<cout<<"s2areais"<return0;
}
Square:
:
Square(doublel)
{
length=l;
}
doubleSquare:
:
Area()
{
returnlength*length;
}
Daan:
s1areais9
S2areais25
//2_16.cpp:
定义控制台应用程序的入口点。
//
#include"stdafx.h"
#include"iostream"//square.cpp
usingnamespacestd;
#include"square.h"
classSquare//square.h
{
private:
doublelength;
public:
Square(doublel);
doubleArea();
voidcopy(Squares);
};
intmain()
{
Squares1(3),s2(5);
cout<<"beforecopy"<cout<<"s1areais"<cout<<"aftercopy"<s1.copy(s2);
cout<<"s1areais"<return0;
}
Square:
:
Square(doublel)
{
length=l;
}
doubleSquare:
:
Area()
{
returnlength*length;
}
voidSquare:
:
copy(Squares)
{
if(this==&s)
return;
*this=s;
}
Daan:
beforecopy
S1areais9
aftercopy
s2areais25
//2_17.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iomanip"//point.cpp
#include"iostream"
usingnamespacestd;
#include"point.h"
classPoint{//point.h
private:
intx,y;
public:
Point(inta,intb);
voidAdd(Pointp);
voidPrint();
};
intmain()
{
Pointob(1,2);
cout<<"beforeadd:
";
ob.Print();
ob.Add(ob);
cout<<"afteradd:
";
ob.Print();
return0;
}
Point:
:
Point(inta,intb)
{
x=a;
y=b;
}
voidPoint:
:
Print()
{
cout<<"x:
"<"<}
voidPoint:
:
Add(Pointp)
{
p.x=p.x+1;
p.y=p.y+1;
}
Daan:
beforeadd:
x:
1,y:
2
Afteradd:
x:
1,y:
2
//2_18.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iomanip"//point.cpp
#include"iostream"
usingnamespacestd;
#include"point.h"
classPoint{//point.h
private:
intx,y;
public:
Point(inta,intb);
voidAdd(Point*p);
voidPrint();
};
intmain()
{
Pointob(1,2);
cout<<"beforeadd:
";
ob.Print();
ob.Add(&ob);
cout<<"afteradd:
";
ob.Print();
return0;
}
Point:
:
Point(inta,intb)
{
x=a;
y=b;
}
voidPoint:
:
Print()
{
cout<<"x:
"<"<}
voidPoint:
:
Add(Point*p)
{
(*p).x=(*p).x+1;//也可以写成:
p->x=p->x+1
(*p).y=(*p).y+1;//也可以写成:
p->y=p->y+1;
}
Daan:
beforeadd:
x:
1,y:
2
Afteradd:
x:
2,y:
3
//2_19.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iomanip"//point.cpp
#include"iostream"
usingnamespacestd;
#include"point.h"
classPoint{//point.h
private:
intx,y;
public:
Point(inta,intb);
voidAdd(Point&p);
voidPrint();
};
intmain()
{
Pointob(1,2);
cout<<"beforeadd:
";
ob.Print();
ob.Add(ob);
cout<<"afteradd:
";
ob.Print();
return0;
}
Point:
:
Point(inta,intb)
{
x=a;
y=b;
}
voidPoint:
:
Print()
{
cout<<"x:
"<"<}
voidPoint:
:
Add(Point&p)
{
p.x=p.x+1;
p.y=p.y+1;
}
Daan:
beforeadd:
x:
1,y:
2
Afteradd:
x:
2,y:
3
//2_20.cpp:
定义控制台应用程序的入口点。
#include"stdafx.h"
#include"iostream"//complex.cpp
usingnamespacestd;
#include"complex.h"
classComplex{//complex.h
private:
double