[C/C++ Basic Exercises] Review Questions Volume 1

When defining a variable, some storage class specifiers of the variable can be omitted

In a function, the storage type specifier can be omitted when defining a variable is auto

Increment and decrement operators can only be used on variables, not on expressions and constants

When defining an array, some of its elements are initialized, and the values ​​of uninitialized elements are not meaningless

Object members are represented in the same way as structure variable members

Constructors can be overloaded, destructors cannot

A friend function is a non-member function in the class body, which can access all members of the class.
Derived classes only inherit public and protected members from the base class, not private members. is ×

Parsing: Private members are also inherited, but cannot be accessed

A virtual function is a member function

A class with pure virtual functions is called an abstract class

Static data member initialization does not use its constructor
Non-static members cannot be directly referenced in static member functions


1. Static members belong to (), in addition to being referenced by object names, they can also be used to refer to () classes, class names are limited

2. Known: double dd[ ][3]={ {1.2, 2.4, 3.6}, {4.8, 5.2},{6.4}}; where dd is the
name of a two-dimensional array, the array has () elements, the type of each element is ().

Answer: If the type is double, there are 3 square brackets, and if it is double, there are 9
3. After running the following program segment, the value of k is () int k=1,n=263; do {k*=n%10 ;n/=10; } while(n);

Answer: k=k*(n/10)=1*3=3,n=n/10=26 k=
3*6=18,n=26/10=2
k=18*2=36,n= 2/10=0, at this time n=0, the loop ends. The value of output k is 36.

4. The object members accessed in the form of "object name. member variable" are limited to the members declared as (); if you want to access other
member variables, you need to use the member function function or () function.

Answer: public friend

5. Given a=4, b=6, c=8, d=9, then "(a++, b>a++&&c>d)?++d: a<b" value is (0)
Answer: If the expression before the question mark is true, the result of the expression is the value of ++d, otherwise it is the value of a<b First look at the
expression before the question mark, it is a comma expression, a++ is executed first, + The value of a after + is 5 but the result of the expression depends on b>a++&&c>d, because c is equal to 8 and d is equal to 9, so the expression is not true, so the final result is the result of a < b, Because the value of a is 6 and the value of b is 6 after two executions of a++, the result of a < b is 0, and the result of the entire expression is 0
6. The following incorrect statement is (B).
A.if(x>y); B.if(x=y)&&(x!=0) x+=y;

7.. In the C++ language, the description about the default value of the parameter is correct (B).
A.The default value of the parameter can only be set when the function is defined
B. When setting the parameter default value, it should be set from right to left
C. When setting the parameter default value, all should be set
D. After setting the default value of the parameter, the calling function can no longer assign a value to the parameter

8. If x is an integer variable, the legal form is (D).
A.&(x+5) B.*x C.&*x D.*&x

9. Among the following characteristics of the copy constructor, (D) is wrong.
A.If there is no copy constructor defined in a class, the system will automatically generate a default
B. The copy constructor has only one parameter, and it is a reference to the object of this class
C. A copy constructor is a member function
D. The name of the copy constructor cannot use the class name
10. In the description of friends, (A) is wrong.
A.A friend function is a member function, which is declared in the class body
B. Friend functions can directly access private members of a class
C. Friend function breaks encapsulation, use it as little as possible
D. All member functions in a friend class are friend functions

11. Among the declarations of the following virtual base classes, the correct one is (D).
A. class virtual B: public A B. virtual class B: public A
C. class B: public A virtual D. class B: virtual public A

12. To achieve dynamic linking, virtual functions must be called through (A).
A.object pointer b. Member name restriction C. object name d. derived class name

13. In the following description about abstract classes, the wrong one is (D).
A.There should be at least one pure virtual function in an abstract class B. Abstract classes can define object pointers and object references
C. An abstract class is usually used as the top-level class in a class D. Derived classes of abstract classes must be concrete classes

14. In the following description, (D) is a feature of an abstract class.
A.Can explain virtual functions B. Constructor overloading is possible
C. Friend functions can be defined D. cannot specify its object


 
#include <iostream.h>
#include <math.h>
class Complex
{p
ublic:
Complex(double r,double i);
~Complex();
double abscomplex();
private:
double real;
double imag;
};
Complex::Complex(double r,double i)
{
cout<<"Executing constructor..."<<endl;
real=r;
imag=i;
cout<<"real="<<real<<",imag="<<imag<<endl;
}C
omplex::~Complex()
{
cout<<"Executing destructor...";
cout<<"real="<<real<<",imag="<<imag <<endl;
}d
ouble Complex::abscomplex()
{
double t;
t=real*real+imag*imag;
return sqrt(t);
}i
nt main()
{
Complex A(3,4);
Complex B(6,8);
cout<<"|A|="<<A.abscomplex()<<endl;
cout<<"|B|="<<B.abscomplex()<<endl;
return 0;
}
Program running results:
Executing constructor...
real=3,imag=4
Executing constructor...
Real=6,imag=8
|A|=5
|B|=1
#include<iostream.h>
class Point
{p
ublic:
Point()
{
x=0;y=0;
cout<<" Call Default Constructor."<<endl;
cout<<x<<","<<y<<endl;
}
Point(int x1,int y1)
{
x=x1;
y=y1;
cout<< " Call Constructor."<<endl;
cout<<x<<","<<y<<endl;
}
~Point()
{
cout<<" Call Destructor."<<endl;
}
int Getx() {return x;}
int Gety() {return y;}
private:
int x,y;
};
void main()
{
cout<<"Object One:"<<endl;
Point *Ptr1=new Point;
delete Ptr1;
cout<<"Object Two:"<<endl;
Ptr1=new Point(1,2);
delete Ptr1;
}
Object One:
Call Default Constructor.
0,0
Call Destructor.
Object Two:
(1 point)
(1 point)
(1 point)
Call Default Constructor.
1,2
Call Destructor.
(1 point)
(1 point)
(1 point

#include <iostream.h>
class BaseA1
{p
ublic:
BaseA1(int i)
{
cout<<"Constructing BaseA1 "<<endl;
x1=i;
} v
oid print()
{
cout<<"x1="<<x1<<endl;
}
protected:
int x1;
};
class BaseA2
{p
ublic:
BaseA2(int j)
{
cout<<"Constructing BaseA2 "<<endl;
x2=j;
} v
oid print()
{
cout<<"x2="<<x2<<endl;
}protected:
int x2;
};
class BaseA3
{p
ublic:
BaseA3()
{
cout<<"Constructing BaseA3"<<endl;
} v
oid print()
{
cout<<"Costructing BaseA3 No Value!"<<endl;
}
};
class MDerivedB: public BaseA2, public BaseA1, public BaseA3
{p
ublic:
MDerivedB(int a, int b, int c, int d):BaseA1(a),A2(d),
A1(c),BaseA2(b){}
void print()
{
BaseA1::print();
BaseA2::print();
BaseA3::print();
}
private:
BaseA1 A1;
BaseA2 A2;
BaseA3 A3;
};
void main()
{
MDerivedB obj(1,2,3,4);
obj.print();
}
程序运行结果:
Constructing BaseA2
Constructing BaseA1
Constructing BaseA3
Constructing BaseA1
Constructing BaseA2
Constructing BaseA3
x1=1
x2=2
Constructing BaseA3 No Value!
 

Define a point class Point, whose private data includes the plane coordinates x and y of the point, and use the parameterized constructor to determine the object coordinates. Write a friend function Distance to calculate the distance between any two points. Define two points P1 and P2 in the main function, the coordinates are (0,0) and (10,20) respectively
, calculate and output the distance between these two points

#include <iostream.h>
#include <math.h>
class Point
{p
ublic:
Point(double x1,double y1)
{
x=x1;
y=y1;
} f
riend double Distance(Point &p1,Point &p2); //友元函数的声明
private:
double x,y;
};
double Distance(Point &p1,Point &p2) //普通函数作为友元函数
{
double deltax,deltay;
deltax=p1.x-p2.x; //访问私有成员
deltay=p1.y-p2.y; //访问私有成员
return sqrt(deltax*deltax+deltay*deltay);
}v
oid main()
{
Point p1(0,0);
Point p2(10,20);
cout<<"以上两给定点之间的距离是:"<<Distance(p1,p2)<<endl;
}

Guess you like

Origin blog.csdn.net/Catherine_77/article/details/128903245