实验与作业汇总
约 5781 字大约 19 分钟
2026-11-10
实验01
01_01.cpp
源文件:01\01_01.cpp
#include<iostream>
using namespace std;
int main()
{
cout<<sizeof 'a'<<" "<<sizeof "a"<<" "<<endl;
char tmp[]="Hello World";
tmp[5]='0';
cout<<tmp<<endl;
tmp[5]=0;
cout<<tmp<<endl;
return 0;
}01_02.cpp
源文件:01\01_02.cpp
#include<iostream>
using namespace std;
int main()
{
int i=5;
cout<<i++<<endl;
cout<<++i<<endl;
cout<<(++i)++<<endl;
cout<<i<<endl;
cout<<(i++,++i,i*3)<<endl;
return 0;
}01_03.cpp
源文件:01\01_03.cpp
#include<iostream>
using namespace std;
int main()
{
int a(2),b(3),c=9;
cout<<a<<"\t"<<b<<'\t'<<c<<endl;
if(a>b || ++c==9){
cout<<a<<"\t"<<b<<'\t'<<c<<endl;
}
cout<<a<<"\t"<<b<<'\t'<<c<<endl;
return 0;
}01_04.cpp
源文件:01\01_04.cpp
#include<iostream>
using namespace std;
int main()
{
int var1(9),var2{var1};
double var3=3.1415926;
bool var4=false;
char var5='0';
cout<<var1<<" "<<var2<<" "<<var3<<" "<<var4<<" "<<var5<<" "<<endl;
var1=var3;
cout<<var1<<endl;
var4=var2-10;
cout<<var4<<endl;
return 0;
}01_05_1.cpp
源文件:01\01_05_1.cpp
#include<iostream>
using namespace std;
int main()
{
int i,sum=0;
cin>>i;
while(i<=10){
sum+=i;
i++;
}
cout<<"sum="<<sum<<endl;
return 0;
}01_05_2.cpp
源文件:01\01_05_2.cpp
#include<iostream>
using namespace std;
int main()
{
int i,sum=0;
cin>>i;
do{
sum+=i;
i++;
}while(i<=10);
cout<<"sum="<<sum<<endl;
return 0;
}01_06_1.cpp
源文件:01\01_06_1.cpp
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<10;i++){
if(i%3==0)
break;
cout<<i<<endl;
}
return 0;
}01_06_2.cpp
源文件:01\01_06_2.cpp
#include<iostream>
using namespace std;
int main()
{
for(int i=1;i<10;i++){
if(i%3==0)
continue;
cout<<i<<endl;
}
return 0;
}01_07.cpp
源文件:01\01_07.cpp
#include<iostream>
using namespace std;
void ShowNum()
{
int a;
cin>>a;
cout<<dec<<"dec:"<<a<<endl;
cout<<oct<<"oct:"<<a<<endl;
cout<<hex<<"oct:"<<a<<endl;
}
int main()
{
ShowNum();
return 0;
}01_08.cpp
源文件:01\01_08.cpp
#include<iostream>
using namespace std;
void ShowScore()
{
double a;
cout<<"�㿼�˶��ٷ֣�(0~100)"<<endl;
cin>>a;
if(a>=90 && a<=100)
cout<<"�ȼ�=����"<<endl;
if(a>=80 && a<90)
cout<<"�ȼ�=����"<<endl;
if(a>=60 && a<80)
cout<<"�ȼ�=�е�"<<endl;
if(a>=0 && a<60)
cout<<"�ȼ�=��"<<endl;
}
int main()
{
ShowScore();
return 0;
}01_09.cpp
源文件:01\01_09.cpp
#include<iostream>
using namespace std;
//����1������
void ShowPic1(char a,int w)
{
//��һ�е��м��е���һ�� ��lineΪ��ǰ�У�
for(int line=0;line<w/2;line++){
for(int i=0;i<w/2-line-(w%2!=0?0:1);i++){
cout<<" ";
}
for(int j=0;j<line*2+1;j++){
cout<<a;
}
if(w%2==0) cout<<a;
cout<<endl;
}
//���
if(w%2!=0){
for(int i=0;i<w;i++)
cout<<a;
cout<<endl;
}
//�м��е���һ�е����һ��
//�˴��ǰ������line������д��
for(int line=w/2-1;line>=0;line--){
for(int i=0;i<w/2-line-(w%2!=0?0:1);i++){
cout<<" ";
}
for(int j=0;j<line*2+1;j++){
cout<<a;
}
if(w%2==0) cout<<a;
cout<<endl;
}
}
//����2������
void ShowPic2(char a,int w)
{
//���һ���������Թ滮�������������
//����x������y
//x+y>=((w%2==0)?(w/2-1):w/2)����������ż���У��У���������
for(int y=0;y<w;y++){
for(int x=0;x<w;x++){
if( x+y>=((w%2==0)?w/2-1:w/2) &&
x+y<=w/2+w-1 &&
x-y>=-w/2 &&
x-y<=w/2)
cout<<a;
else
cout<<" ";
}
cout<<endl;
}
}
//�Ի�����
void ask()
{
int line;char a;
cout<<"��ѡ��һ��ͼ����";
cin>>a;
cout<<"�������ӡ������";
cin>>line;
cout<<"����1���������"<<endl;
ShowPic1(a,line);
cout<<"����2���������"<<endl;
ShowPic2(a,line);
}
int main()
{
for(int i=0;i<2;i++){
ask();
}
return 0;
}实验02
001.cpp
源文件:02\001.cpp
#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
class Point
{
private:
int m_x;
int m_y;
public:
Point(int x, int y) : m_x(x), m_y(y)
{
cout << "Point的初始化列表构造函数调用" << m_x << " " << m_y << endl;
}
~Point()
{
cout << "Point的析构函数调用" << m_x << " " << m_y << endl;
}
Point(Point &P)
{
m_x = P.m_x;
m_y = P.m_y;
cout << "Point的拷贝构造函数调用" << m_x << " " << m_y << endl;
}
int getX()
{
return m_x;
}
int getY()
{
return m_y;
}
};
class Line
{
private:
Point m_s, m_e;
public:
Line(Point s, Point e) : m_s(s), m_e(e)
{
cout << "Line的初始化列表构造函数调用" << endl;
}
Line(Line &L) : m_s(L.m_s), m_e(L.m_e)
{
cout << "Line的拷贝构造函数调用" << endl;
}
~Line()
{
cout << "Line的析构函数调用" << endl;
}
double getDis()
{
double x = double(m_s.getX() - m_e.getX());
double y = double(m_s.getY() - m_e.getY());
return sqrt(x * x - y * y);
}
};
int main()
{
Point p1(1, 2), p2(3, 4);
cout << "---------------------" << endl;
Line L1(p1, p2);
cout << "---------------------" << endl;
Line L2(L1);
return 0;
}02_01.cpp
源文件:02\02_01.cpp
#include <iostream>
using namespace std;
class ABC
{
int a;
public:
ABC(int aa) : a(aa) {} //缺少:
};
void test()
{
}
int main()
{
test();
system("pause");
return 0;
}02_02.cpp
源文件:02\02_02.cpp
#include <iostream>
using namespace std;
class Aton
{
int X, Y;
protected:
int zx, zy;
public:
void init(int i, int j)
{
zx = i;
zy = j;
}
Aton(int i, int j, int n = 0, int m = 0)
{
X = i, Y = j, zx = m, zy = n;
}
int getX() { return X; }
int getY() { return Y; }
};
int main()
{
Aton A(25, 20, 3, 5);
A.init(5, 9);
cout << A.getX() << endl;//X和Y均为private访问控制类型,无法在类外被直接访问,可通过类内函数访问
cout << A.getY() << endl;
}02_03.cpp
源文件:02\02_03.cpp
#include <iostream>
using namespace std;
class Test
{
int x, y;
public:
Test(int i = 0, int j = 0) //构造函数不应该有返回值类型
{
x = i;
y = j;
}
void show();
}; //类定义末尾未加分号
void Test::show() //应该对类Test的成员函数进行定义
{
cout << "x=" << x << ",y=" << y << endl;
}
int main() // C++程序的主函数必须有返回值
{
Test a;
a.show();
Test b(1);
b.show();
Test c(2, 4);
c.show();
return 0;
}02_04.cpp
源文件:02\02_04.cpp
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point(int xx = 0, int yy = 0);
Point(Point &p);
};
Point::Point(int xx, int yy) : x(xx), y(yy)
{
cout << "Calling constructor of Point!" << endl;
}
Point::Point(Point &p)
{
x = p.x;
y = p.y;
cout << "Calling the copy constructor of Point!" << endl;
}
void test1()
{
Point p1(3, 4);
Point p2(p1);
Point p3;
cout << "\ntest1 end\n";
}
void test2()
{
Point p1(3, 4);
Point p2;
p2 = p1;
Point p3;
cout << "\ntest2 end\n";
}
int main()
{
test1();
test2();
return 0;
}02_05_01.cpp
源文件:02\02_05_01.cpp
#include <iostream>
using namespace std;
class Point
{
private:
int x, y;
public:
Point()
{
x = 1;
y = 1;
}
Point(int tx, int ty)
{
x = tx;
y = ty;
}
~Point() { cout << "Point " << x << ',' << y << " is deleted." << endl; }
};
int main()
{
Point obj1;
Point obj2(10, 20);
return 0;
}02_05_02.cpp
源文件:02\02_05_02.cpp
#include <iostream>
using namespace std;
class XCF
{
int a;
public:
XCF(int aa = 0) : a(aa) { cout << 1; }
XCF(XCF &x)
{
a = x.a;
cout << 2;
}
~XCF() { cout << a; }
int Geta() { return a; }
};
int main()
{
XCF d1(5), d2(d1);
cout << d2.Geta();
return 0;
}02_05_03.cpp
源文件:02\02_05_03.cpp
#include <iostream>
using namespace std;
class Point
{
int x, y;
public:
Point(int x1 = 0, int y1 = 0) : x(x1), y(y1) {}
int get(){return x + y;}
};
class Circle
{
Point center;
int radius;
public:
Circle(int cx,int cy,int r=1):center(cx,cy),radius(r){}
int get(){return center.get()+radius;}
};
int main()
{
Circle c1(1,2,3),c2(4,5);
cout<<c1.get()<<endl;
cout<<c2.get()<<endl;
return 0;
}02_06.cpp
源文件:02\02_06.cpp
#include <iostream>
using namespace std;
class Monitor
{
public:
Monitor(char t) : type(t) {}
void display() { cout << "The type of monitor is " << type << endl; }
private:
char type;
};
class Computer
{
public:
Computer(int i, char c) : id(i), mon(c) {}
void Display()
{
cout << "The computer is " << id << endl;
mon.display();
}
private:
int id;
Monitor mon;
};
int main()
{
Computer myComputer(101, 'B');
myComputer.Display();
return 0;
}02_07.cpp
源文件:02\02_07.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Segment
{
public:
// 构造函数,初始化线段起点坐标(xx1, yy1),终点坐标(xx2, yy2),长度length
Segment(float xx1 = 0.0, float yy1 = 0.0, float xx2 = 1.0, float yy2 = 1.0)
: x1(xx1), y1(yy1), x2(xx2), y2(yy2)
{
length = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
count++;
}
// 复制构造函数,初始化线段起点坐标(xx1, yy1),终点坐标(xx2, yy2),长度length
Segment(Segment &seg)
: x1(seg.x1), y1(seg.y1), x2(seg.x2), y2(seg.y2), length(seg.length) { count++; }
// 将线段起点平移到(newX, newY)
void moveTo(float newX, float newY)
{
x1 = newX;
y1 = newY;
length = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
float getx1() { return x1; } // 返回x1
float gety1() { return y1; } // 返回y1
float getx2() { return x2; } // 返回x2
float gety2() { return y2; } // 返回y2
double getLen() { return length; } // 返回线段的长度
// 判断与另一条线段的长度是否相等
bool isEqual(Segment &s)
{
if (length == s.length)
return true;
else
return false;
}
int show()
{ // 显示目前已创建的Point对象数
cout << "count=" << count << endl;
return 1;
}
//~Segment() { /*count--;*/ } //析构函数
private:
float x1, y1, x2, y2; // 线段起点坐标(x1, y1)和终点坐标(x2, y2)
double length; // 线段的长度
static int count; //记录线段的个数
};
int Segment::count = 0;
int main()
{
Segment s1(1, 1, 4, 4), s2;
s2.show();
cout << "the length of s1:" << s1.getLen() << endl;
cout << "the length of s2:" << s2.getLen() << endl;
cout << "s1.length==s2.length?" << s2.isEqual(s1) << endl;
s1.moveTo(3, 3);
cout << "the length of s1:" << s1.getLen() << endl;
cout << "the length of s2:" << s2.getLen() << endl;
cout << "s1.length==s2.length?" << s2.isEqual(s1) << endl;
Segment s3(s1);
s3.show();
for (int i = 0; i < 4; i++)
{
Segment tmp(s1);
tmp.show();
}
return 0;
}02_08.cpp
源文件:02\02_08.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int xx = 0, int yy = 0)
{
x = xx;
y = yy;
cout << "Point(" << xx << "," << yy << ")"
<< " is created" << endl;
// cout << "Point(" << x << "," << y << ")"
// << " is ++++" << endl;
}
Point(Point &p);
~Point() { cout << "Point(" << x << "," << y << ")"
<< " is eliminated" << endl; }
int getX() { return x; }
int getY() { return y; }
private:
int x, y;
};
Point::Point(Point &p)
{
x = p.x;
y = p.y;
cout << "Calling the copy constructor of Point" << endl;
// cout << "Point(" << x << "," << y << ")"
// << " is ----" << endl;
}
class Line
{
public:
Line(Point xp1, Point xp2);
Line(Line &l);
double getLen() { return len; }
~Line() { cout << "The Line from (" << p1.getX() << "," << p1.getY() << ")"
<< " to (" << p2.getX() << "," << p2.getY() << ")"
<< " is eliminated" << endl; }
private:
Point p1, p2;
double len;
};
Line::Line(Point xp1, Point xp2) : p1(xp1), p2(xp2)
{
cout << "Calling constructor of Line" << endl;
double x = static_cast<double>(p1.getX() - p2.getX());
double y = static_cast<double>(p1.getY() - p2.getY());
len = sqrt(x * x + y * y);
}
Line::Line(Line &l) : p1(l.p1), p2(l.p2)
{
cout << "Calling the copy constructor of Line" << endl;
len = l.len;
}
int main()
{
Point myp1(1, 1), myp2(4, 5);
Line line(myp1, myp2);
Line line2(line);
cout << "The length of the line is: ";
cout << line.getLen() << endl;
cout << "The length of the line2 is: ";
cout << line2.getLen() << endl;
system("pause");
return 0;
}第6章课后作业
6_20.cpp
源文件:homework\6\6_20.cpp
#include <iostream>
using namespace std;
class XCF
{
int a;
public:
XCF(int aa = 0) : a(aa) { cout << "1"; }
XCF(XCF &x)
{
a = x.a;
cout << "2";
}
~XCF() { cout << a; }
int Geta() { return a; }
};
int main()
{
XCF dl(5), d2(dl);
XCF *pd = new XCF(8);
cout << pd->Geta();
delete pd;
return 0;
}6_21.cpp
源文件:homework\6\6_21.cpp
#include <iostream>
using namespace std;
class CD
{
public:
~CD() { cout << 'C'; }
private:
char name[80];
};
int main()
{
CD a, *b, d[2];
return 0;
}6_22.cpp
源文件:homework\6\6_22.cpp
#include <iostream>
using namespace std;
int main()
{
int a[6] = {23, 15, 64, 33, 40, 58};
int s1, s2;
s1 = s2 = a[0];
for (int *p = a + 1; p < a + 6; p++)
{
if (s1 > *p)
s1 = *p;
if (s2 < *p)
s2 = *p;
}
cout << s1 + s2 << endl;
return 0;
}6_26.cpp
源文件:homework\6\6_26.cpp
#include <iostream>
using namespace std;
int main()
{
int A[4][3] = {{1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10}};
cout << A[0][2] << " " << A[2][2] << endl;
return 0;
}6_28.cpp
源文件:homework\6\6_28.cpp
#include <iostream>
using namespace std;
int main()
{
int arr[2][2] = {{9, 8}, {7, 6}};
int *p = arr[0] + 1;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << &arr[i][j] << " ";
}
cout << "\n";
}
cout << "**arr = " << **arr << endl;
cout<< "*(*(arr + 1) + 1) = " << *(*(arr + 1) + 1) << endl;
cout<< "arr[0] = " << arr[0] << endl;
cout << "arr[0]+1 = " << arr[0] + 1 << endl;
cout << *p << endl;
return 0;
}6_30.cpp
源文件:homework\6\6_30.cpp
#include <iostream>
using namespace std;
int main()
{
int a[] = {1, 2, 3, 4, 5, 6}, *p = a;
cout << (*++p)++ << endl;
return 0;
}6_31.cpp
源文件:homework\6\6_31.cpp
/*
请将如下程序补充完整,使得程序运行时的输出结果为:
010
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线)。
试题程序:
*/
#include <iostream>
using namespace std;
class myClass
{
static int a;
public:
myClass() { a++; }
~myClass() { a--; }
/**********FILL**********/
static int getA();//函数原型
//___【1】___;
};
/**********FILL**********/
int myClass::a=0;//静态变量
//___【2】___;
int myClass::getA() { return a; }
int main()
{
/**********FILL**********/
cout << myClass::getA();
//cout << ___【3】___;
myClass d[10];
cout << d[0].getA() << endl;
return 0;
}6_32.cpp
源文件:homework\6\6_32.cpp
/*
ARRAY类用于存放一个一维int型数组,计算并输出当前数组中各元素的最大值、最小值和平均值。
其成员声明已给出,请参照注释,完成ARRAY类的定义。
注意:部分源程序给出,仅允许在注释"Begin"和"End"之间填写内容,不得改动main函数和其他已有的任何内容。
试题程序:
*/
#include <iostream>
#include <fstream>
using namespace std;
const int N = 100;
class ARRAY
{
int data[N]; // 数组data用于存储待处理的数据,N为常量
int num; // 当前元素的个数
int max, min; // 元素的最大值、最小值
float average; // 元素的平均值
public:
ARRAY(int n); // 构造函数,n为元素个数
void input(); // 输入n个整数
void process(); //计算data数组中的最大值、最小值和平均值
void print()
{ // 输出当前数组中的元素及其最大值、最小值和平均值
cout << "共有" << num << "个元素: ";
for (int i = 0; i < num; i++)
cout << data[i] << ' ';
cout << endl;
cout << "max=" << max << endl;
cout << "min=" << min << endl;
cout << "average=" << average << endl
<< endl;
}
};
/*******Begin*******/
//构造函数
ARRAY::ARRAY(int n) : num(n) {}
//接收数据函数
void ARRAY::input()
{
for (int i = 0; i < num; i++)
{
cin >> data[i];
}
}
//计算最大值最小值平均值
void ARRAY::process()
{
int sum=min=max=data[0];
// min = data[0];
// max = data[0];
// int sum = data[0];
for (int i = 1; i < num; i++)
{
if (data[i] < min)
min = data[i];
if (data[i] > max)
max = data[i];
sum += data[i];
}
average = 1.0 * sum / num;
}
/*******End*********/
int main()
{
int n;
cin >> n; // 输入待处理元素个数
ARRAY arr(n);
arr.input();
arr.process();
arr.print();
return 0;
}6_33.cpp
源文件:homework\6\6_33.cpp
/*
函数find的功能是在N个整数中找出最小值,并将最小值移到第一个位置上(如果存在多个,则只移动第一个),其他元素保持相对位置不变。例如:输入整数8 5 2 9 2,则输出结果为:2 8 5 9 2。
请改正程序中指定位置的错误。
注意:只允许修改注释"ERROR"的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;
const int N = 5;
void find(int *a)
{
int min = 0, temp;
for (int i = 1; i < N; i++)
/**********ERROR**********/
if (a[min] > a[i])
min = i;
// if (a[min] < a[i])
// min = i;
temp = a[min];
int j = min;
while (j > 0)
{
/**********ERROR**********/
a[j] = a[j-1];
//a[j] = a[j + 1];
j--;
}
/**********ERROR**********/
a[0] = temp;
//a[0] = a[min];
}
int main()
{
int a[N];
for (int i = 0; i < N; i++)
cin >> a[i];
/**********ERROR**********/
find(a);
//find(a[N]);
for (int i = 0; i < N; i++)
cout << a[i] << ' ';
cout << endl;
return 0;
}第7章课后作业
7_15.cpp
源文件:homework\7\7_15.cpp
#include <iostream>
using namespace std;
class Base
{
public:
void print() { cout << "Base"; }
};
class Derived : public Base
{
public:
void print(){ cout << "Derived"; }
};
int main()
{
Base b;
b.print();
Derived d;
d.print();
return 0;
}7_16.cpp
源文件:homework\7\7_16.cpp
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout << "This is Class A!" << endl;
}
private:
int x, y;
};
class B : public A
{
public:
void display()
{
cout << "This is Class B!" << endl;
}
private:
int i, j;
};
int main()
{
A a1;
B b1;
b1.display();
a1 = b1;
a1.display();
A &rf = b1;
rf.display();
A *p;
B b;
p = &b;
p->display();
return 0;
}7_17.cpp
源文件:homework\7\7_17.cpp
#include <iostream>
using namespace std;
class Base1
{
public:
void display() const
{
cout << "Base1::display()" << endl;
}
};
class Base2 : public Base1
{
public:
void display() const
{
cout << "Base2::display()" << endl;
}
};
class Derived : public Base2
{
public:
void display() const
{
cout << "Derived::display()" << endl;
}
};
void fun(Base1 *p)
{
p->display();
}
int main()
{
Base1 b1;
Base2 b2;
Derived d1;
fun(&b1);
fun(&b2);
fun(&d1);
return 0;
}7_2_11.cpp
源文件:homework\7\7_2_11.cpp
#include <iostream>
using namespace std;
class Base
{
public:
int m, n;
Base(int i, int j) : m(i), n(j) {}
};
class Derived : public Base
{
public:
int m, k;
Derived(int i, int j) : Base(i, j), m(i + 1), k(j + 1) {}
};
int main()
{
Derived d(1, 5);
cout << d.m << d.k << d.n;
return 0;
}7_2_12.cpp
源文件:homework\7\7_2_12.cpp
#include <iostream>
using namespace std;
class Base
{
public:
Base() {}
void f() { cout << "fun of Base" << endl; }
};
class D1 : public Base
{
public:
D1() {}
void f() { cout << "fun of D1" << endl; }
};
class D2 : public D1
{
public:
void f() { cout << "fun of D2" << endl; }
};
void fun(Base &b) { b.f(); }
int main()
{
D2 d2;
fun(d2);
return 0;
}7_2_5.cpp
源文件:homework\7\7_2_5.cpp
#include <iostream>
using namespace std;
class XX
{
int xx;
public:
XX() : xx(0) { cout << 'A'; }
XX(int n) : xx(n) { cout << 'B'; }
};
class YY : public XX
{
int yy;
public:
YY() : yy(0) { cout << yy; }
YY(int n) : XX(n + 1), yy(n) { cout << yy; }
YY(int m, int n) : XX(m), yy(n) { cout << yy; }
};
main()
{
YY y1(0, 0);
cout << endl;
YY y2(1);
cout << endl;
YY y3(0);
cout << endl;
YY y4;
}7_2_6.cpp
源文件:homework\7\7_2_6.cpp
#include <iostream>
using namespace std;
class AA
{
public:
AA() { cout << '1'; }
};
class BB : public AA
{
int k;
public:
BB() : k(0) { cout << '2'; }
BB(int n) : k(n) { cout << '3'; }
};
int main()
{
BB b(4), c;
return 0;
}第8章课后作业
8_13.cpp
源文件:homework\8\8_13.cpp
#include <iostream>
using namespace std;
class A
{
public:
virtual void f() { cout << 1; }
void g() { cout << 2; }
};
class B : public A
{
public:
virtual void f() { cout << 3; }
void g() { cout << 4; }
};
void show(A &a)
{
a.f();
a.g();
}
int main()
{
B b;
show(b);
return 0;
}8_18.cpp
源文件:homework\8\8_18.cpp
#include <iostream>
using namespace std;
class Instrument
{
public:
virtual void Display() = 0;
};
class Piano : public Instrument
{
public:
void Display()
{ /*函数体程序略*/
}
};
int main()
{
//Instrument s;//抽象类不允许被实例化
Instrument *p = 0;
}8_19.cpp
源文件:homework\8\8_19.cpp
#include <iostream>
using namespace std;
class Base
{
int x;
public:
Base(int b) : x(b) {}
virtual void display() { cout << x << endl; }
};
class Derived : public Base
{
int y;
public:
Derived(int d) : Base(d), y(d) {}
void display() { cout << y << endl; }
};
int main()
{
Base b(1);
Derived d(2);
Base *p = &d;
b.display();
d.display();
p->display();
return 0;
}8_20.cpp
源文件:homework\8\8_20.cpp
#include <iostream>
using namespace std;
class Animal
{
public:
virtual char *getType() const { return "Animal"; }
virtual char *getVoice() const { return "Voice"; }
};
class Dog : public Animal
{
public:
char *getType() const { return "Dog"; }
char *getVoice() const { return "Woof"; }
};
void type(Animal &a) { cout << a.getType(); }
void speak(Animal a) { cout << a.getVoice(); }
int main()
{
Dog d;
type(d);
cout << "Speak";
speak(d);
cout << endl;
return 0;
}8_21.cpp
源文件:homework\8\8_21.cpp
#include <iostream>
using namespace std;
class Base
{
public:
Base() {}
virtual void f() { cout << "A"; }
};
class D1 : public Base
{
public:
D1() {}
// virtual void f() { cout << "AD1"; }
virtual void f() const { cout << "B"; }
};
class D2 : public D1
{
public:
virtual void f() { cout << "C"; }
};
void fun(Base &b) { b.f(); }
int main()
{
Base b;
D1 d1;
D2 d2;
fun(b);
fun(d1);
fun(d2);
b.f();
d1.f();
d2.f();
return 0;
}8_22_0.cpp
源文件:homework\8\8_22_0.cpp
/*
完善程序, 使其运行时的输出结果如下 :
f1 function of derive
f2 function of base
f4 function of base
*/
#include <iostream>
using namespace std;
class base
{
public:
/*____(1)____*/ virtual void f1() { cout << "f1 function of base" << endl; }
virtual void f2() { cout << "f2 function of base " << endl; }
// void f2(int x) { cout << "f2 x function of derive " << endl; }
void f4() { cout << "f4 function of base " << endl; }
};
class derive : public base
{
public:
void f1() { cout << "f1 function of derive" << endl; }
void f2(int x) { cout << "f2 function of derive " << endl; }
void f4() { cout << "f4 function of derive" << endl; }
};
int main()
{
base *p;
/*____(2)____*/ derive obj;
/*____(3)____*/ p = &obj;
p->f1();
p->f2();
p->f4();
return 0;
}8_22_1.cpp
源文件:homework\8\8_22_1.cpp
/*
完善程序, 使其运行时的输出结果如下 :
f1 function of derive
f2 function of base
f4 function of base
*/
#include <iostream>
using namespace std;
class base
{
public:
virtual void f1()
{
cout << "f1 function of base" << endl; //虚函数
}
virtual void f2()
{
cout << "f2 function of base " << endl; //虚函数
}
void f4()
{
cout << "f4 function of base " << endl; //普通成员函数
}
};
class derive : public base
{
public:
void f1()
{
cout << "f1 function of derive" << endl; //覆盖,虚函数,可多态
}
void f2(int x)
{
cout << "f2 function of derive " << endl; //同名隐藏,没有被覆盖,不多态
}
void f4()
{
cout << "f4 function of derive" << endl; //同名隐藏,没有被覆盖,不多态
}
};
int main()
{
derive obj;
cout << "基类指针指向派生类对象--有虚函数覆盖,则有多态" << endl;
base *p;
p = &obj;
p->f1();
p->f2();
p->f4();
cout << "派生类对象赋值给基类对象--类型兼容性原则" << endl;
base b;
b = obj;
b.f1();
b.f2();
b.f4();
cout << "派生类对象--同名函数隐藏,看不到基类中的函数。可用base::看" << endl;
obj.f1();
obj.f2(1);
obj.f4();
cout << "派生类指针指向派生类对象--同名函数隐藏,看不到基类中的函数。可用base::看" << endl;
derive *p1;
p1 = &obj;
p1->f1();
p1->f2(1);
p1->f4();
return 0;
}期中练习
00.cpp
源文件:midExam\00.cpp
#include <iostream>
#include <cmath>
using namespace std;
class Point
{
public:
Point(int x = 0, int y = 0) : x(x), y(y) { count++; }
// Point(Point &p)
// {
// x = p.x;
// y = p.y;
// count++;
// }
//构造函数形参应写为常引用,常引用可绑定到常对象或普通对象
Point(const Point &p)
{
x = p.x;
y = p.y;
count++;
}
~Point() { count--; }
int getX() { return x; }
int getY() { return y; }
static void showCount()
{
cout << "current count=" << count << endl;
}
//将get_dist()在Point类内声明为友元函数后,可在函数内通过对象访问私有数据成员
friend double get_dist(Point p1, Point p2);
void print()
{
cout << "x=" << x << "\t"
<< "y=" << y << endl;
}
//常对象不可访问普通成员函数,可访问常成员函数。可在类内再定义一个功能类似的const成员函数,系统会根据对象的类型自动选择对应的成员函数(重载)
void print()const
{
cout << "x=" << x << "\t"
<< "y=" << y << endl;
}
private:
int x, y;
static int count; //静态数据在类内声明,类外初始化
};
int Point::count = 0; //静态数据在类内声明,类外初始化
//函数的定义在声明之后。
//将get_dist()在Point类内声明为友元函数后,可在函数内通过对象访问私有数据成员
double get_dist(Point p1, Point p2)
{
double tmp_x = p1.x - p2.x;
double tmp_y = p1.y - p2.y;
return sqrt(tmp_x * tmp_x + tmp_y * tmp_y);
}
int main()
{
Point::showCount();
Point p1;
Point p2(p1);
Point p3(3, 4);
Point::showCount();
cout << "the distence is:" << get_dist(p3, p1) << endl;
Point p4(10, 11);
p4.print();
const Point p5(66, 6);
p5.print(); //常对象不可访问普通成员函数,可访问常成员函数。可在类内再定义一个功能类似的const成员函数,系统会根据对象的类型自动选择对应的成员函数(重载)
Point p6(p5);
p6.print();
Point::showCount();
return 0;
}001.cpp
源文件:midExam\001.cpp
#include <iostream>
#include <cmath>
using namespace std;
class B1
{
int b1;
public:
B1(int i)
{
b1 = i;
cout << "B1::" << b1 << endl;
}
~B1()
{
cout << "B1::" << b1 << endl;
}
};
class B2
{
int b2;
public:
B2(int i)
{
b2 = i;
cout << "B2::" << b2 << endl;
}
~B2()
{
cout << "B3::" << b2 << endl;
}
};
class B3
{
int b3;
public:
B3(int i)
{
b3 = i;
cout << "B3::" << b3 << endl;
}
~B3()
{
cout << "B3::" << b3 << endl;
}
};
class D
{
int d;
B2 b2;
B1 b1;
B3 b3;
public:
D(int i, int j, int k, int l) : b1(i), b2(j), b3(k)
{
d = l;
cout << "D::" << d << endl;
}
};
int main()
{
D d(1, 2, 3, 4);
return 0;
}004.cpp
源文件:midExam\004.cpp
#include <iostream>
#include <cmath>
using namespace std;
class A
{
public:
A(int xx) : x(xx) { count++; }
//11111111111111111111111111
A(const A &b)
{
x = b.x;
count++;
}
~A() { count--; }
static void print();
private:
int x;
//222222222222222222222222222222
static int count;
};
//33333333333333333
int A::count=0;
void A::print()
{
cout<<count<<endl;
}
int main()
{
A a1(2);
A::print();
const A a2(3);
A::print();
//44444444444444444444444
const A &a3 = a2;
A::print();
A a4(a2);
A::print();
return 0;
}实践课
ppt_1.cpp
源文件:shijian\ppt_1.cpp
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s1 = "面向对象", s2 = "程序设计";
cout << s1 << s2 << endl;
s1 += s2;
cout << s1 << endl;
return 0;
}ppt_2.cpp
源文件:shijian\ppt_2.cpp
#include <iostream>
using namespace std;
void swap(int x, int &y)
{
int temp = x;
x = y;
y = temp;
}
int main()
{
int a(5), b;
b = a / 2;
cout << "a=" << a << ", "
<< "b=" << b << endl;
swap(a, b);
cout << "a=" << a << ", "
<< "b=" << b << endl;
return 0;
}ppt_3.cpp
源文件:shijian\ppt_3.cpp
#include <iostream>
using namespace std;
int main()
{
int v[4] = {};
for (int i = 0; i < 3; ++i)
v[i] = i * i * i;
for (auto e : v)
cout << e;
cout << endl;
return 0;
}ppt_4.cpp
源文件:shijian\ppt_4.cpp
#include <iostream>
using namespace std;
int a = 2;
int main()
{
int a(10);
{
int a;
a = 20;
::a = 5;
cout << "a=" << a << endl;
}
cout << "a=" << a << endl;
cout << "a=" << ::a << endl;
return 0;
}ppt_5.cpp
源文件:shijian\ppt_5.cpp
#include <iostream>
using namespace std;
class B1
{
int b1;
public:
B1(int i = 0)
{
b1 = i;
cout << b1;
}
};
class B2
{
int b2;
public:
B2(int i)
{
b2 = i;
cout << b2;
}
};
class B3
{
int b3;
public:
B3(int i)
{
b3 = i;
cout << b3 << endl;
}
};
class D
{
int d;
B2 b2;
B1 b1;
B3 b3;
public:
D(int i, int j, int k) : b2(i), b3(j)
{
d = k;
cout << "D::" << d << endl;
}
};
int main()
{
D d(1, 2, 3);
return 0;
}shijian_01.cpp
源文件:shijian\shijian_01.cpp
#include <iostream>
using namespace std;
class Base
{
public:
virtual void virt()
{
cout << "this is virt of Base!!!" << endl;
}
Base()
{
cout << "Base---constructor" << endl;
virt();
}
~Base()
{
cout << "Base---destructor" << endl;
virt();
}
};
class Derived : public Base
{
public:
void virt()
{
cout << "this is virt of Derived!!!" << endl;
}
Derived()
{
cout << "Derived---constructor" << endl;
virt();
}
~Derived()
{
cout << "Derived---destructor" << endl;
virt();
}
};
int main()
{
Derived d1;
Base b1=d1;
// system("pause");
return 0;
}word_1.cpp
源文件:shijian\word_1.cpp
/*
请改正程序中指定位置的错误,使程序的输出结果如下:
Base0 display
Base1 display
Derived display
注意:只允许修改注释“ERROR”的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序清单:
*/
#include <iostream>
using namespace std;
class Base0
{
public:
/**********ERROR**********/
virtual void display() const
{
cout << "Base0 display" << endl;
}
};
/**********ERROR**********/
class Base1 : public Base0
{
public:
void display() const
{
cout << "Base1 display" << endl;
}
};
class Derived : public Base1
{
public:
/**********ERROR**********/
void display() const
{
cout << "Derived display" << endl;
}
};
/**********ERROR**********/
void fun(Base0 &b)
{
b.display();
}
int main()
{
Base0 b0;
Base1 b1;
Derived d;
fun(b0);
fun(b1);
fun(d);
return 0;
}word_2.cpp
源文件:shijian\word_2.cpp
/*
请改正程序中指定位置的错误,使程序的输出结果如下:
4, 5
4, 5, 20
注意:只允许修改注释“ERROR”的下一行,不得改动程序中的其他内容,也不允许增加或删减语句。
源程序:
*/
#include <iostream>
using namespace std;
class A
{
/**********ERROR 1**********/
public:
int n, m;
public:
void set(int a, int b)
{
m = a;
n = b;
}
void show() { cout << m << ", " << n << endl; }
};
/**********ERROR 2**********/
class B : public A
{
int s;
public:
void set() { s = m * n; }
void shows() { cout << m << ", " << n << ", " << s << endl; }
};
int main()
{
B b;
/**********ERROR 3**********/
b.A::set(4, 5);
b.show();
b.set();
/**********ERROR 4**********/
b.shows();
return 0;
}word_3.cpp
源文件:shijian\word_3.cpp
/*
下面程序在定义分数(fract)类的基础上,重载复合赋值运算符"+="。请将程序补充完整,使程序运行时的输出结果为41/28。
注意:仅允许在指定的下划线处填写代码,不得改动程序中的其他内容(需删除下划线部分)。
试题源程序:
*/
#include <iostream>
using namespace std;
class fract
{
int den; //分子
int num; //分母
public:
fract(int d = 0, int n = 1) : den(d), num(n) {}
fract &operator+=(const fract &);
/**********FILL**********/
void show(){ cout << den << '/' << num << endl; }
};
/**********FILL**********/
fract & fract::operator+=(const fract &f)
{
this->den = this->den * f.num + this->num * f.den;
this->num *= f.num;
/**********FILL**********/
return *this;
}
int main()
{
fract fr(3, 4), fr2(5, 7);
/**********FILL**********/
fr+=fr2;
fr.show();
return 0;
}