问题:对象之间有几种关系,分别是什么?
简单来讲,对象间的关系可以描述为:
has-a 关系:包含关系,A对象中包含B对象。这是在一个类中嵌入另一个类的实例实现的。
is -a 关系:继承关系,B对象是对A对象的继承。这是由继承技术实现的。
has-access-to 关系:可访问关系,即B可以访问A中的数据成员。这是由友元类和引用实现的。
问题:什么是友元类?
友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。
当希望一个类可以存取另一个类的私有成员时,可以将该类声明为另一类的友元类。定义友元类的语句格式如下:
friend class 类名;
其中:friend和class是关键字,类名必须是程序中的一个已定义过的类。
例如,以下语句说明类B是类A的友元类:
class A
{
…
public:
friend class B;
…
};
经过以上说明后,类B的所有成员函数都是类A的友元函数,能存取类A的私有成员和保护成员。
使用友元类时注意:
(1) 友元关系不能被继承。
(2) 友元关系是单向的,不具有交换性,不具有传递性。
问题:引用的使用需要注意一些什么?
引用在创建之时必须初始化,或在其声明中完成。对于成员引用,在构造方法的前导中完成初始化。或者在调用一个带有引用的方法时,在调用之时完成。
问题:右移位运算符的工作方式有什么不同
?
对于无符号数,1011>>1 == 0101,对于有符号类型,1011>>1 == 1101,因为无论怎么移位,是不会改变其符号的,对于任何一个数字来说,移位运算符,右移都是除以2,左移都是乘以2(假设不溢出)。
分享到:
相关推荐
I learned C++11 the hard way. Because I didn’t follow the standardization as it was happening I started to look at C++11 about two years ago. I really had trouble understanding it. But the people on ...
Although it is not as big a step as C++11, it contains a large number of small and valuable language and library features, which again will change the way we program in C++. This applies to both ...
The journey of C++ from its early days as a pre-processor to its current form as a sophisticated and highly capable language is documented in a timeline that covers each step along the way. This ...
We’ve now lived over 20 years with C++ templates, but the C++ programmers’ community still regularly finds new fundamental insights into the way they can fit in our software development needs....
The latest versions of C++ have seen programmers change the way they code, giving up on the old-fashioned C-style programming and adopting modern C++ instead. Beginning with the modern language ...
the set of features I explain in each chapter is based on the way that I see a particular type of problem being solved with the language. In this way I hope to move you, a little at a time, from ...
This book takes you on a tour of the Pi 2 hardware and all of the fantastic things that you can do to create innovative and useful projects with your Pi. Start with creating a workstation that does ...
The C++17 release includes changes that impact the way you work with C++; this new fourth edition covers them all, including nested namespaces, structured bindings, string_view, template argument ...
With this book by your side, you are well on your way to writing applications in both versions of C++ and becoming a successful C++ programmer. Ivor Horton's Beginning Visual C++ 2010: Teaches the ...
C++ has come a long way and is in use in every area of the industry. Fast, efficient, and flexible, it is used to solve many problems. The upcoming version of C++ will see programmers change the way ...
Note that this guide is not a C++ tutorial: we assume that the reader is familiar with the language. Header Files In general, every .cc file should have an associated .h file. There are some common ...
It focuses on the elements of C++ programming that you are most likely to encounter and examines features and techniques that help solve real-world programming challenges. Essential C++ presents the ...
With its innovative approach to teaching the language, Accelerated C++ will challenge readers in the right way. It suggests that you don’t need to learn C to be productive in C++. Written in an ...
This is a hands-on book for programmers who want to learn about how C++ is used in the financial industry. The book concentrates on the parts of the language that are more frequently used to write ...
Learning C++ by doing is the best way—so try the rich variety of code samples in this book hands-on and help yourself improve your programming proficiency. These code snippets have been tested using ...
C++ has come a long way and is in use in every area of the industry. Fast, efficient, and flexible, it is used to solve many problems. The upcoming version of C++ will see programmers change the way ...
Templates: The Hard Way Templates: The C++ Way Function Specialization Class Templates Class Specialization Implementation Details Advanced Features Summary Programming Exercises 25. Standard ...