When it comes to object-oriented, everyone's first reaction should be its three main features: encapsulation, inheritance, and polymorphism. So let's start with a brief look at these three features:
(1) Encapsulation: encapsulation, that is, to encapsulate objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.
The properties of a member in a class in C + + are: public, protected, private, and the access rights of these three properties are reduced in turn.
(2) Inheritance: Inheritance refers to the ability to use all the functionality of an existing class and to extend these functions without rewriting the original class.
(3) Polymorphism: Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type. To achieve polymorphism, there are two ways, covering, overloading.
Overwrite refers to the practice of redefining the virtual function of a parent class by a subclass.
overloading, which means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps the number of parameters is different, perhaps the parameter types are different, perhaps the two are different).
Let's start our study today.
1. What class member functions are generated by the C + + Hollow class by default?
Answer:
For an empty class, the compiler produces 4 member functions by default:
(1) Default constructor
(2) destructor
(3) Copy constructor
(4) Assignment function
2. Can structures have constructors, destructors, and member functions? If so, what is the difference between a struct and a class?
Answer:
The difference is that the variable by default in class is public by default in the Private,struct variable. class inheritance defaults to private inheritance, while struct inheritance defaults to public inheritance. Structs can have constructors, destructors, or even multiple inheritance, and so on. The struct in C + + is in fact the same as the class meaning, the only difference is that the default access control in the struct is public,class the default access control is private. The only meaning of the struct keyword in C + + is for C programmers to have a sense of belonging in order to make the C + + compiler compatible with projects previously developed with C.
3. What is the result of the following program printing?
123456789101112131415161718192021 |
#include<iostream>
using
namespace
std;
class
base
{
private
:
int
m_i;
int
m_j;
public
:
base(
int
i ) : m_j(i),m_i(m_j) {}
base() : m_j(0),m_i(m_j){}
int
get_i() {
return
m_i;}
int
get_j() {
return
m_j;}
};
int
main ()
{
base obj(98);
cout << obj.get_i() <<endl<< obj.get_j() <<endl;
return
0;
}
|
Analysis: The result of the subject is "98,98". However, the declaration of the member variable is m_i first, then m_j; the initialization order of the initialization list is performed based on the order in which the member variables are declared, so the m_i is initialized first, but M_j is not initialized at this time, and m_i is given a random value. Change the declaration order of the member variables to get the desired result.
Answer:
The output is the first random number, and the second one is 98.
4. Is the following class statement correct? Why?
1234 |
class A { const int Size = 0; }; |
Analysis: There is a problem of member variables in this procedural problem. Constants must be initialized in the constructor's initializer list or set to static.
Answer:
The correct procedure is as follows:
1234567 |
class A { A() { const int Size = 1; } }; |
Or:
1234 |
class A { static const int Size = 1; }; |
5, the destructor can be virtual type, the constructor is not, why?
Answer:
Virtual functions Adopt A method of virtual invocation. A virtual call is a mechanism that works with only part of the information, specifically allowing us to invoke a function that knows only the interface and does not know its exact object type. But if you want to create an object, you're bound to know the exact type of the object, so the constructor cannot be virtual.
6, if the virtual function is very effective, can we declare each function as a virtual function?
Answer:
No, this is because virtual functions have a price: because each virtual function object must maintain a V table, there is a system overhead when using virtual functions. If you are only a small class and do not derive other classes, you do not need to use virtual functions at all.
7, please see the following paragraph of the procedure:
12345678910111213141516171819202122232425262728293031323334 |
#include<iostream>
using
namespace
std;
class
B
{
private
:
int data;
public
:
B()
{
cout<<
"defualt constructor"
<<endl;
}
~B()
{
cout<<
"destructed "
<<endl;
}
B(
int
i) : data(i)
{
cout<<
"constructed by parameter"
<<data<<endl;
}
};
B Play( B b )
{
return
b;
}
int
main ()
{
B temp = Play(5);
return
0;
}
|
Problem:
(1) What is the result of the program output? Why is there such an output?
(2) B (int i): data (i), what is the professional term for this usage?
(3) Play (5), the formal parameter type is a class, and 5 is a constant, so is it legal to write? Why?
Answer:
(1) The output results are as follows:
123 |
constructed by parameter //在Play(5)处,5通过隐含的类型转换调用了B::B( int i ) destructed //Play(5) 返回时,参数的析构函数被调用 destructed //temp的析构函数被调用;temp的构造函数调用的是编译器生存的拷贝构造函数 |
(2) constructor to be parameterized, followed by a member variable initialization list (member initialization lists)
(3) legal. constructor of a single parameter if you do not add the explicit keyword, an implicit type conversion is defined, and adding the explicit keyword eliminates this implicit conversion.
8. Write the constructor, destructor and assignment function of the class String.
The prototype of a known class String is:
12345678910 |
class
String
{
public
:
String(
const
char
*str = NULL);
//普通构造函数
String(
const
String &other);
//拷贝构造函数
~String(
void
);
//析构函数
String & operate =(
const
String &other);
//赋值函数
private
:
char
*m_data;
//用于保存字符串
};
|
Answer:
1, the destructor of String:
1234 |
String::~String( void ) { delete [] m_data; } |
2, the constructor of String:
1234567891011121314 |
String::String(
const
char
*str)
{
if
(NULL==str)
{
m_data =
new
char
[1];
*m_data =
‘\0‘
;
}
else
{
int
length =
strlen
(str);
m_data =
new
char
[length+1];
strcpy
(m_data,str);
}
}
|
3, copy constructor for string:
123456 |
< Code class= "CPP Plain" >string::string ( const string &other) { &NBSP;&NBSP;&NBSP;&NBSP; int length = strlen &NBSP;&NBSP;&NBSP;&NBSP; m_data = new char [length+1]; &NBSP;&NBSP;&NBSP;&NBSP; strcpy } |
4, the assignment function of string:
123456789101112 |
String & String::operate =(
const
String &other)
{
if
(
this
== &other)
//检查自复制
{
return *
this
;
}
delete
[] m_data;
//释放原有的内存资源
int
length=
strlen
(other.m_data);
//分配新内存资源,并复制内容
m_data =
new
char
[length+1];
strcpy
(m_data,other.m_data);
return
*
this
;
//返回本对象的引用
}
|
9. What is the difference between overloading and covering?
Answer:
Virtual functions are always overridden in derived classes, which are called "override".
Override means that a derived class overrides a virtual function of a base class, and the overridden function must have a consistent parameter table and return value. The word "override" seems to have had no proper Chinese vocabulary to correspond to. Some people are translated as "overlay", but also appropriate.
Overload convention idiomatic is translated as "overloaded", which is to write a function that has the same name as the existing function but has a different parameter table. For example, a function can accept an integer as an argument, or it can receive a floating-point number as an argument. Overloading is not an object-oriented programming, but a syntax rule, and overloading is not directly related to polymorphism.
Object-oriented [C + + face question]