13年题。
This commit is contained in:
33
OOP/13Exam/2.1.cpp
Normal file
33
OOP/13Exam/2.1.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// 在下面基类Base和派生类Derived中增加成员函数show,使得程序运行结果如下:
|
||||
// Base
|
||||
// Derived
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base {
|
||||
public:
|
||||
// _________________________;
|
||||
|
||||
virtual void show() {
|
||||
cout << "Base" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
class Derived : public Base {
|
||||
public:
|
||||
// __________________________;
|
||||
|
||||
virtual void show() {
|
||||
cout << "Derived" << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Base b, *ptr;
|
||||
Derived d;
|
||||
ptr = &b;
|
||||
ptr->show();
|
||||
ptr = &d;
|
||||
ptr->show();
|
||||
return 0;
|
||||
};
|
||||
39
OOP/13Exam/2.2.cpp
Normal file
39
OOP/13Exam/2.2.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
// 以下程序是定义一个计数器类counter,对其重载运算符“+”。请编写运算符重载函数,使得程序输出结果如下:
|
||||
// n = 5
|
||||
// n = 10
|
||||
// n = 15
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class counter {
|
||||
private:
|
||||
int n;
|
||||
|
||||
public:
|
||||
counter() {
|
||||
n = 0;
|
||||
}
|
||||
counter(int i) {
|
||||
n = i;
|
||||
}
|
||||
|
||||
//_____________________
|
||||
// {_____________________}
|
||||
|
||||
counter operator+(const counter &otherCounter) {
|
||||
return counter(this->n + otherCounter.n);
|
||||
}
|
||||
|
||||
void disp() {
|
||||
cout << "n=" << n << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
counter c1(5), c2(10), c3;
|
||||
c3 = c1 + c2;
|
||||
c1.disp();
|
||||
c2.disp();
|
||||
c3.disp();
|
||||
return 0;
|
||||
}
|
||||
26
OOP/13Exam/2.3.cpp
Normal file
26
OOP/13Exam/2.3.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// 在给出的时间类Time中,增加show_time函数,以时:分:秒的格式显示时间。
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Time {
|
||||
public:
|
||||
Time(int h, int m, int s) {
|
||||
hour = h, minute = m, sec = s;
|
||||
}
|
||||
|
||||
// ______________________
|
||||
// {_______________________}
|
||||
|
||||
void show_time() {
|
||||
cout << hour << ":" << minute << ":" << sec << endl;
|
||||
}
|
||||
|
||||
private:
|
||||
int hour, minute, sec;
|
||||
};
|
||||
|
||||
int main() {
|
||||
Time T(5, 0, 0);
|
||||
T.show_time();
|
||||
return 0;
|
||||
}
|
||||
27
OOP/13Exam/2.4.cpp
Normal file
27
OOP/13Exam/2.4.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
// 已知文件test.txt的内容如下:
|
||||
// Good Morning!
|
||||
// Good Night!
|
||||
// Good Luck!
|
||||
// 补充下面程序,使得输出结果如下:
|
||||
// 1:Good Morning!
|
||||
// 2:Good Night!
|
||||
// 3:Good Luck!
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
const int size = 100;
|
||||
int main() {
|
||||
char buf[::size];
|
||||
// _______________
|
||||
|
||||
ifstream in("test.txt", ios::in);
|
||||
|
||||
int i = 1;
|
||||
while (in.getline(buf, ::size)) {
|
||||
// ____________________
|
||||
|
||||
cout << i <<":" << buf << endl;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
33
OOP/13Exam/2.5.cpp
Normal file
33
OOP/13Exam/2.5.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
// 下面定义了一个shape抽象类,派生出Rectangle类,用于计算Rectangle类对象的面积Area()。请补充完整下面的程序,使程序输出60。
|
||||
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Shape {
|
||||
public:
|
||||
// _________________
|
||||
|
||||
virtual void Area() = 0;
|
||||
};
|
||||
|
||||
class Rectangle : public Shape {
|
||||
public:
|
||||
// _________________
|
||||
|
||||
int width, height;
|
||||
|
||||
Rectangle(int _width, int _height) : width(_width), height(_height) {
|
||||
}
|
||||
|
||||
void Area() {
|
||||
cout << width * height << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Shape *sp;
|
||||
Rectangle re1(10, 6);
|
||||
sp = &re1;
|
||||
sp->Area();
|
||||
return 0;
|
||||
}
|
||||
42
OOP/13Exam/3.1.cpp
Normal file
42
OOP/13Exam/3.1.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class test {
|
||||
private:
|
||||
int num;
|
||||
float fl;
|
||||
|
||||
public:
|
||||
test();
|
||||
int getint() {
|
||||
return num;
|
||||
}
|
||||
float getfloat() {
|
||||
return fl;
|
||||
}
|
||||
~test();
|
||||
};
|
||||
|
||||
test::test() {
|
||||
cout << "Initializing default" << endl;
|
||||
num = 0;
|
||||
fl = 0.0;
|
||||
}
|
||||
|
||||
test::~test() {
|
||||
cout << "Destructor is active" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
test array[2];
|
||||
cout << array[1].getint() << "" << array[1].getfloat() << endl;
|
||||
return 0;
|
||||
};
|
||||
|
||||
/*
|
||||
Initializing default
|
||||
Initializing default
|
||||
00
|
||||
Destructor is active
|
||||
Destructor is active
|
||||
*/
|
||||
43
OOP/13Exam/3.2.cpp
Normal file
43
OOP/13Exam/3.2.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class A {
|
||||
public:
|
||||
A() {
|
||||
cout << "A::A() called.\n";
|
||||
}
|
||||
virtual ~A() {
|
||||
cout << "A::~A() called.\n";
|
||||
}
|
||||
};
|
||||
|
||||
class B: public A {
|
||||
public:
|
||||
B(int i) {
|
||||
cout << "B::B() called.\n";
|
||||
buf = new char[i];
|
||||
}
|
||||
virtual ~B() {
|
||||
delete [] buf;
|
||||
cout << "B::~B() called.\n";
|
||||
}
|
||||
|
||||
private:
|
||||
char *buf;
|
||||
};
|
||||
|
||||
void fun(A *a) {
|
||||
delete a;
|
||||
}
|
||||
|
||||
int main() {
|
||||
A *a = new B(15);
|
||||
fun(a);
|
||||
}
|
||||
|
||||
/*
|
||||
A::A() called.
|
||||
B::B() called.
|
||||
B::~B() called.
|
||||
A::~A() called.
|
||||
*/
|
||||
31
OOP/13Exam/3.3.cpp
Normal file
31
OOP/13Exam/3.3.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
class Base {
|
||||
private:
|
||||
char c;
|
||||
|
||||
public:
|
||||
Base(char n): c(n) {}
|
||||
virtual ~Base() {
|
||||
cout << c;
|
||||
}
|
||||
};
|
||||
|
||||
class Der:public Base {
|
||||
private:
|
||||
char c;
|
||||
public:
|
||||
Der(char n):Base(n + 1), c(n) {}
|
||||
~Der(){
|
||||
cout << c;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Der('X');
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
XY
|
||||
*/
|
||||
68
OOP/13Exam/3.4.cpp
Normal file
68
OOP/13Exam/3.4.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
class complex {
|
||||
public:
|
||||
complex(double r = 0.0, double i = 0.0) {
|
||||
this->r = r;
|
||||
this->i = i;
|
||||
cout << "Constructor!";
|
||||
display();
|
||||
}
|
||||
complex(complex &c) : r(c.r), i(c.i) {
|
||||
cout << "Copy Constructor!";
|
||||
display();
|
||||
};
|
||||
complex operator+(complex c2);
|
||||
complex operator-(complex c2);
|
||||
void display();
|
||||
|
||||
private:
|
||||
double r;
|
||||
double i;
|
||||
};
|
||||
|
||||
complex complex::operator+(complex c2) {
|
||||
complex result(r + c2.r, i + c2.i);
|
||||
return result;
|
||||
}
|
||||
|
||||
complex complex::operator-(complex c2) {
|
||||
complex result(r - c2.r, i - c2.i);
|
||||
return result;
|
||||
// return complex(r - c2.r, i - c2.i);
|
||||
}
|
||||
|
||||
void complex::display() {
|
||||
cout << "(" << r << "," << i << ")" << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
complex c1(5, 4), c2(2, 10), c3;
|
||||
cout << "c1=";
|
||||
c1.display();
|
||||
cout << "c2=";
|
||||
c2.display();
|
||||
c3 = c1 - c2;
|
||||
cout << "c3=c1-c2=";
|
||||
c3.display();
|
||||
c3 = c1 + c2;
|
||||
cout << "c3=c1+c2=";
|
||||
c3.display();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Constructor!(5,4)
|
||||
Constructor!(2,10)
|
||||
Constructor!(0,0)
|
||||
c1=(5,4)
|
||||
c2=(2,10)
|
||||
Copy Constructor!(2,10)
|
||||
Constructor!(3,-6)
|
||||
c3=c1-c2=(3,-6)
|
||||
Copy Constructor!(2,10)
|
||||
Constructor!(7,14)
|
||||
c3=c1+c2=(7,14)
|
||||
*/
|
||||
56
OOP/13Exam/3.5.cpp
Normal file
56
OOP/13Exam/3.5.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
class Book {
|
||||
char *title;
|
||||
char *author;
|
||||
int numsold;
|
||||
|
||||
public:
|
||||
Book() {
|
||||
}
|
||||
Book(const char *str1, const char *str2, const int num) {
|
||||
int len = strlen(str1);
|
||||
title = new char[len + 1];
|
||||
strcpy(title, str1);
|
||||
len = strlen(str2);
|
||||
author = new char[len + 1];
|
||||
strcpy(author, str2);
|
||||
numsold = num;
|
||||
}
|
||||
void setbook(const char *str1, const char *str2, const int num) {
|
||||
int len = strlen(str1);
|
||||
title = new char[len + 1];
|
||||
strcpy(title, str1);
|
||||
len = strlen(str2);
|
||||
author = new char[len + 1];
|
||||
strcpy(author, str2);
|
||||
numsold = num;
|
||||
}
|
||||
~Book() {
|
||||
delete title;
|
||||
delete author;
|
||||
}
|
||||
void print(ostream &output) {
|
||||
output << "book:" << title << endl;
|
||||
output << "author:" << author << endl;
|
||||
output << "total:" << numsold << endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Book obj1("Physics", "LiFu", 200), obj2;
|
||||
obj1.print(cout);
|
||||
obj2.setbook("C++ Programming", "SunJuason", 300);
|
||||
obj2.print(cout);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
book:Physics
|
||||
author:LiFu
|
||||
total:200
|
||||
book:C++ Programming
|
||||
author:SunJuason
|
||||
total:300
|
||||
*/
|
||||
3
OOP/13Exam/test.txt
Normal file
3
OOP/13Exam/test.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
Good Morning!
|
||||
Good Night!
|
||||
Good Luck!
|
||||
Reference in New Issue
Block a user