typedef
B. mycase
C. typeid
D. typename
area
B. _age
C. -xy
D. w123
case
B. de。fault
C. c_ase
D. a.b
void main() {
//int num;
int& ref = num;
ref = ref + 100;
num = num + 50;
}
答:变量num没有声明。
2. 分析如下主程序中的错误。
void main() {
int x = 58, y = 98;
const int *p = &x;
y = *p;
*p = 65;
p = &y;
}
答:code:5: error:只读变量不可更改。
3. 分析如下主程序中的错误。
void main() {
int x = 58, y = 98, z = 55;
int* const p = &x;
*p = 65;
p = &y;
z = *p;
}
答:code:5: error:常量指针不可更改。
65
和66
。#include
using namespace std;
int main()
{
int temp1 = 65, temp2 = 66;
cout << (char)temp1 << " " << (char)temp2 << endl;
cout << temp1 << " " << temp2 << endl;
return 0;
}
int
型变量分配100个整形量空间的程序。#include
using namespace std;
int main()
{
int *ptr = new int[100];
delete []ptr;
return 0;
}
15
个float
值,用指针把它们存放在一个存储块里,然后输出这些值的和以及最小值。#include
using namespace std;
const int num = 15;
int main()
{
float temp[num];
float count = 0.0f;
float mini = 0.0f;
cout << "Please insert 15 numbers." << endl;
for (int i = 0; i < num; ++i)
{
cin >> temp[i];
count += temp[i];
if (temp[i] < mini || i == 0)
{
mini = temp[i];
}
}
cout << "equal: " << count << endl;
cout << "mininum: " << mini << endl;
return 0;
}
int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
4
的位置,将数组a
复制给数组b
,然后将数组a
的内容反转,再查找4
的位置,最后分别输出数组a
和b
的内容。#include
using namespace std;
const int num = 8;
int find(int a[], int key, int n)
{
for (int i = 0; i < n; ++i)
{
if (a[i] == key)
{
return i + 1;
}
}
return 0;
}
void copy(int a[],int b[], int n)
{
for (int i = 0; i < n; ++i)
{
b[i] = a[i];
}
}
void reverse(int a[], int n)
{
int temp;
int i, j = n - 1, m = (n - 1) / 2;
for (i = 0; i <= m; ++i)
{
j = n - 1 - i;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
cout << find(a, 4, num) << endl;
int b[num];
copy(a, b, num);
reverse(a, num);
cout << find(a, 4, num) << endl;
for (int i = 0; i < num; ++i)
{
cout << a[i] << " ";
}
cout << endl;
for (int i = 0; i < num; ++i)
{
cout << b[i] << " ";
}
cout << endl;
return 0;
}
string
类建立对象的不正确方式是( D )。 string str(" OK");
B. string str=" OK";
C. string str;
D. string str='OK';
#include
#include
using std::string;
using std::cout;
using std::endl;
string push_back(string str1, string str2)
{
string::iterator iter = str2.begin();
for (; iter != str2.end(); ++iter)
{
str1.push_back(*iter);
}
return str1;
}
int main()
{
string str1("Cconnect ");
string str2("String!");
string temp(str1);
cout << "+ or +=: " << str1 + str2 << endl;
cout << "append: " << str1.append(str2) << endl;
str1 = temp;
cout << "push_back: " << push_back(str1, str2) << endl;
cout << "others:insert() or str[] .." << endl;
return 0;
}
string
的对象str
的内容为“We are here!”,使用多种方法输出字符“h”。#include
#include
using std::string;
using std::cout;
using std::endl;
int main()
{
string str("We are here!");
cout << str[str.find('h')] << endl;
cout << str.substr(str.find('h'), 1) << endl;
return 0;
}
double abc(double, char);
”表示的含义是 声明一个返回double类型的,且第一个参数为double和第二个参数为char的函数abc 。char& func(char, int)
的含义是 声明一个返回char&引用类型的,且第一个参数为char和第二个参为int的函数func 。fun
的返回值是指针,其中一个参数是字符,另一个是参数int
类型的引用,声明fun
的函数的原型是int * fun(char, int &);
。string input(const int);
B. string input(int &);
C. string* input(int *);
D. string input(string &);
template
B. template
C. template
D. template
template
T fun(T x) {
T y;
//y = x * x - T;
y = x * x - y;
return y;
}
答:code:4: error: 'T'
不是有效的变量或常量。
2. 找出下面程序中的错误并改正之。
#include
template
//Type max(T x, y)
Type max(Type x,Type y)
{ return (x > y) ? (x) : (y); }
答:code:3: error:'T'
和'y'
不是有效的变量
3. 找出下面程序中的错误并改正之。
//void change(const string &s)
void change(string &s)
{ s = s + "pig!"; }
//}
void main() {
string str(" it is a");
change(str);
}
答:code:1: error:常量s
的值不能改变
#include
#include
//大于0时,方程ax^2 + bx + c = 0有2个相同的解
void solver1(int a, int b, int c) {
double x1, x2, temp;
temp = b * b - 4 * a * c;
x1 = (-b + sqrt(temp)) / (2 * a);
x2 = (-b + sqrt(temp)) / (2 * a);
std::cout << x1 << " " << x2 << std::endl;
}
//等于0时,方程ax^2 + bx + c = 0有2个相同的解
void solver2(int a, int b, int c) {
double x1, x2, temp;
x1 = -b / (2 * a);
x2 = 0;
std::cout << x1 << " " << x2 << std::endl;
}
//小于0时,方程ax^2 + bx + c = 0无解
void solver3(int a, int b, int c) {
std::cout << "null" << std::endl;
}
int main()
{
int a = 0, b = 0, c = 0;
std::cout << "一元二次方程:" << "ax^2 + bx + c = 0" << std::endl;
std::cout << "请输入a, b, c:(用空格断开)" << std::endl;
std::cin >> a >> b >> c;
int delta = b * b - 4 * a * c;
if(delta > 0) {
solver1(a, b, c);
} else if(delta == 0) {
solver2(a, b, c);
} else {
solver3(a, b, c);
}
return 0;
}
up(ch)
,如字符变量ch
是小写字母就转换成大写字母并通过up
返回,否则字符ch
不改变。要求在短小而完全的程序中显示这个程序是怎样被调用的。#include
char up(char ch) {
return toupper(ch);
}
int main()
{
char ch;
std::cout << "Plase enter a character:";
std::cin >> ch;
std::cout << up(ch) << std::endl;
return 0;
}
r
和整数n
两个参数的函数并输出r
的n
次幂。#include
double exponentiation(double r, int n) {
double result = 1.0;
for(int i = 0; i < n; ++i) {
result *= r;
}
return result;
}
int main()
{
double r = 0.0;
int n = 0;
std::cout << "Please enter r and n:" << std::endl;
std::cin >> r >> n;
std::cout << "r^n:" << exponentiation(r, n) << std::endl;
return 0;
}
C
和整型参数N
的函数,让它显示出由字符C
组成的三角形。其方式为第1行有1个字符C
,第2行有2个字符C
等等。#include
void print_triangle(char c, int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
std::cout << c;
}
std::cout << std::endl;
}
}
int main()
{
char c;
int n;
std::cout << "Plase enter c and n:" << std::endl;
std::cin >> c >> n;
print_triangle(c, n);
return 0;
}
strlen()
,再用strlen()
函数编写一个函数revers(s)
的倒序递归程序,使字符串s逆序。#include
#include
int strlen(char *str) {
int len = 0;
while(str[len] != '\0') {
len++;
}
return len;
}
void revers(char *str) {
char c;
int loc, len;
len = strlen(str);
loc = len / 2 - 1;
while(loc >= 0) {
c = *(str + loc);
*(str + loc) = *(str + len - loc - 1);
*(str + len - loc - 1) = c;
loc--;
}
str[len] = '\0';
}
int main()
{
char str[] = {"123456789ABCDEF"};
std::cout << "str's lenth : " << strlen(str) << std::endl;
std::cout << "str's : " << str << std::endl;
revers(str);
std::cout << "str's revers : " << str << std::endl;
}
#include
template
void sort(T a[], int len) {
T temp;
for(int i = 0; i < len; i++) {
temp = a[i];
for(int j = i + 1; j < len; j++) {
if(temp > a[j]) {
a[i] = a[j];
a[j] = temp;
temp = a[i];
}
}
}
}
int main()
{
int a[3] = {0, 2, 1};
sort(a, 3);
for(int i = 0; i < 3; i++) {
std::cout << a[i] << " " << std::endl;
}
return 0;
}
#include
template
T sum(T a[], int len) {
T temp;
for(int i = 0; i < len; i++) {
temp += a[i];
}
return temp;
}
int main()
{
int a[5] = {1, 3, 5, 7, 9};
std::cout << sum(a, 5) << std::endl;
return 0;
}
#include
template
T sum(T a[], int len) {
T temp;
for(int i = 0; i < len; i++) {
temp += a[i];
}
return temp;
}
template
T sum(T a[], int len, T b[], int len2) {
return sum(a, len) + sum(b, len2);
}
int main()
{
int a[5] = {1, 3, 5, 7, 9};
int b[5] = {2, 4, 6, 8, 10};
std::cout << sum(a, 5, b, 5) << std::endl;
return 0;
}
private
和protected
的地址。fun
,则其复制构造函数的原型可声明为 fun(fun&);
和 fun(const fun&);
两种形式。while
B. union
C. class
D. for
void
,应该理解为无参,正确答案应为:~A::A();
~A::A(void)
B. void ~A::A(参数)
C. ~A::A(参数)
D. void ~A::A()
new
运算符不调用构造函数 B. 构造函数一定调用new
运算符 new
运算符,然后调用构造函数进行初始化 D. 当用new
运算符动态产生类的对象时,new
运算符也自动调用构造函数class base {
int *p;
public:
//base(int a) { p = &a };
base(int a) { p = &a; }
//int Getx() { return m; }
int Getx() { return *p; }
//~base() { delete p }
~base() { delete p; }
};
答:code:4: error:
语句应以';'
结尾,方法定义的大括号后不应以';'
结尾。 code:5: error:
不能使用未定义的'm'
。 code:6: error:
语句应以';'
结尾。
2. 找出以下程序中的错误,并说明错在何处。
#include
class Point {
int x;
public:
void init(int a) { Setx(a); }
int Getx() { return x; }
//int Setx(int a) { x = a; }
void Setx(int a) { x = a;}
//}
};
void main() {
Point A;
//A.init(24, 56);
A.init(24);
cout << "A.x = " << A.Getx() << endl;
}
答:code:8: error:
类要以';'
结尾。 code:7: warning:
没有返回值产生警告。 code:10: error:
未初始化的对象A
,对象A
没有匹配的init
方法。
#include
class base {
private:
int m, n;
public:
void init(int M, int N) { m = M; n = N; }
void print() { std::cout << "2 * 68 - 55 = " << 2 * m - n << std::endl; }
};
void main() {
base a;
a.init(68, 55);
a.print();
}
测试结果:2∗68−55=812∗68−55=81
2. 完成下面类中的成员函数的定义。
class Point {
private:
int m, n;
public:
Point(int, int);
Point(Point&);
};
Point::Point(int a, int b)
{
m = a;
n = b;
}
Point::Point(Point &t)
{
m = t.m;
n = t.n;
}
#include
class base {
private:
int a, b;
public:
~base() { cout << "Destry..." << a << "," << b << endl; }
base(int a, int b) : b(b), a(a)
{ cout << "初始化..." << a << "," << b << endl; }
};
答:此题编写的不完整,缺少了主程序,但此题考查的是构造函数和析构函数的调用顺序,所以可得: 初始化...a,b
Destry...a,b
其中变量a
和b
应替换为实际值。
2. 分析下面程序的输出结果。
class base {
private:
int x;
public:
void setx(int a) { x = a; }
int getx() { return x; }
};
void main() {
int *p;
base a;
a.setx(55);
p = new int(a.getx());
cout << *p;
}
答:55
Point
,再设计一个矩形类,矩形类使用Point
类的两个坐标点作为矩形对角顶点,并可以输出4个坐标值和面积。使用测试程序验证程序。#include
class Point {
private:
float x, y;
public:
Point() { x = 0; y = 0; }
Point(float X, float Y) { x = X; y = Y; }
float getX() { return x; };
float getY() { return y; };
void setX(float X) { x = X; };
void setY(float Y) { y = Y; };
};
class Rectangular {
private:
Point point[4];
public:
Rectangular(Point a, Point d) {
point[0] = a;
point[1].setX(d.getX());
point[1].setY(a.getY());
point[2] = d;
point[3].setX(a.getX());
point[3].setY(d.getY());
}
void printPointsLocation() {
for(int i = 0; i < 4; ++i) {
std::cout << point[i].getX() << ", " << point[i].getY() << std::endl;
}
}
float getArea() {
float height, width, area;
height = point[0].getY() - point[3].getY();
width = point[1].getX() - point[0].getX();
area = height * width;
return area;
}
void printArea() { std::cout << "area:" << getArea() << std::endl; }
};
int main()
{
Point a(1.0, 10.0), b(10.0, 1.0);
Rectangular rect(a, b);
rect.printPointsLocation();
rect.printArea();
return 0;
}
#include
#include
class Line {
private:
int x1, y1, x2, y2;
public:
Line(int X1, int Y1, int X2, int Y2) {
x1 = X1; y1 = Y1; x2 = X2; y2 = Y2;
}
inline double getLenght() { return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); }
inline void printPoints() {
std::cout << "Point1: " << x1 << ", " << y1 << std::endl;
std::cout << "Point2: " << x2 << ", " << y2 << std::endl;
}
inline void printLenght() {
std::cout << "Line.Lenght: " << getLenght() << std::endl;
}
};
int main()
{
Line line(10, 10, 60, 80);
line.printPoints();
line.printLenght();
return 0;
}
Line
,使用继承的方法组成Rectangle
类。 Line
,使用聚合的方法组成Rectangle
类。 const
写在函数参数列表之后,函数体之前,说明该函数是 成员函数。double
B. static
C. float
D. int
print()
函数是一个类的常成员函数,它无返回值,下列表示中正确的是( )。 void print() const
B. const void print()
C. void const print()
D. void print(const)
class base {
int number;
static int a;
public:
void set(int i, int j);
static int geta() { return a; }
static int getn() { return number; }
void show() { cout << number << "," << a << endl; }
};
Test
,改正主程序中的错误。void main() {
test *two[2] = { test(4, 5), test(6, 8) };
for(i = 0 ; i < 2 ; i++)
delete two[i];
}
完成下面程序并给出运行结果。
#include
using namespace std;
class test {
int X:
public:
test(int a) { X = a; }
int GetX() { return X; }
};
void main() {
test *p, a[2][3] = { {1, 2, 3}, {4, 5, 6} };
for( ; i <= 6; p++) {
if((p - a[0]) % 3 == 0) cout << endl;
cout << p->GetX() << " ";
}
}
class base {
int m;
public:
base(int a) : m(a);
int Getx() { return m; }
};
class derived : private base {
public:
derived(int b) : m(b) {};
};
Show
的方法解决名字冲突。#include
class Base1 {
protected:
int m;
public:
void Show() { cout << m << endl; }
};
class Base2 {
protected:
int n;
public:
void Show() { cout << n << endl; }
};
class Derived : public Base1, public Base2 {
public:
void Set(int x, int y) { m = x; n = y; }
};
void main() {
Derived Obj;
Obj.Set(45, 87);
Obj.Show();
Obj.Base1::Show();
}
capacity()
capacity()
的长度自动增加 D. 如果数量已满,当需要增加一个元素时,capacity()
的长度每次自动增加一个typedef
定义逆向泛型指针的语句是 。template
class TAnyTemp {
T x;
public:
TAnyTemp(T X) : x(X) {}
void getx() { return x; }
}
class Point {
int x, y;
public:
Point(int a, int b) { x = a; y = b; }
};
template
class Line : public Point {
T x2, y2;
public:
Line(T a, T b, T c, T d) : Point(a, b) { x2 = c; y2 = d; }
};
a
,其内容为1 3 5 7 9 2 4 6 8 10
。先对数组进行升序排序,再使用它产生向量b
,然后再在向量的尾部追回11
,并按降序输出向量的内容和capacity()
内容。virtual void f(void);
B. virtual void f(void) = 0;
C. void f(void) = 0;
D. virtual void f(void) {}
#include
using namespace std;
class base {
int x;
public:
base(int a) { x = a; cout << "base..." << x << endl; }
base(base &t) { x = t; cout << "base copy..." << x << endl; }
virtual ~base() { cout << "~derived..." << y << endl; }
};
class derived : public base {
int y;
public:
derived(int a, int b) : base(a)
{ y = b; cout << "derived..." << y << endl; }
derived(derived &t) : base(t)
{ y = t.y; cout << "derived..." << y << endl; }
~derived() { cout << "derived..." << y << endl; }
};
void main() {
base *pb = new derived(52, 44);
base a(*pb);
delete pb;
}
#include
using namespace std;
class A {
public:
virtual void fa(void) { cout << "A::fa" << endl; }
void fb(void) { cout << "A::fb" << endl; }
};
class B :public A {
public:
void fa(void) { cout << "B::fa" << endl; }
void fb(void) { cout << "B::fb" << endl; }
}
void main() {
A *pa = new A;
A *pb = new B:
pa->fa();
pb->fb();
pb->fa();
pb->A::fa();
((B*)pb)->fb();
}
找出下面程序中的错误,使其输出结果如下: 58
18, 18
源程序如下:
#include
class base
{
int m;
public:
base(int a) { m = a;}
int Getm() { return m; }
virtual void show() { cout << Getm() << endl; }
};
class derived : public base
{
int n;
public:
derived(int a, int b) : base(a) { n = b;};
virtual void show() { cout << Getm() << "," << n << endl; }
};
void print(base p)
{ p.show(); }
void main() {
base A(58);
derived B(18, 28);
print(A);
print(B);
}
仔细阅读下面的程序,根据输出结果分别给出基类base
和派生类derived
类中没有定义的内联函数void display(void)const
。
#include
class base
{
int i;
int j;
public:
base(int I, int J) : i(I), j(J) { display(); }
int getI(void) { return i; }
int getJ(void) { return j; }
};
class derived : public base
{
int k;
public:
derived(int I, int J, int K) : base(I, J), k(K) { display(); }
//定义display
};
void main(void)
{
base b3(8, 9);
derived d1(10, 20, 5);
}
程序输出: i=8 i=9
i=10 i=20
i=10 i=20 k=5
i+k=15 j+k=25
width
函数控制输出宽度,含有这个成员函数的是( )。setprecision(int n)
必须包含的头文件是( )。ios_base::right
含义是 。ofstream fout;
fout.open("Text.txt");
分析下面程序的输出结果。
分析程序功能。
void main() {
std::cout << -25.89F << " ";
std::cout << 25.89f << std::endl;
}
******12345 54321******
。源程序如下:#include
using namespace std;
class FUN {
friend ostream (ostream&, FUN);
}fun;
ostream (ostream& os, FUN f)
{
os.self(ios::left);
return os;
}
void main() {
cout << setfill('*') << setw(10) << 12345 << " ";
cout << fun << setw(10) << 54321 << endl;
}
TEST
中读出字符并写到文件TEST1
里,要求均附加错误检查。text
“的磁盘文件中保存。输入的字符串以”$
“结束。Cow
属性,练习使用模板实现包含的设计方法。Cow
属性,练习使用模板实现继承的设计方法。