[C++] Class (part 1)
The fundamental ideas behind classes are data abstraction and encapsulation.
Data abstraction is a programming technical that relies on the separation of interface(declaration) and implementation(definition)
A class designer desings and implements a class for users of that class.
In this class, the user is a progrmmer, not the utilmate user of the application.
Every member must be declared inside its class, and we can difine a member's body either inside or outside of the class body.
Objects that are const, and references or pointers to const objects may call only const member functions.
The compiler process class in two steps
- the member declarations are compiled first
- the member function bodies are processed.
The name of a member defined outside the class must inlucde the class name.
class Sales_data::arg_print() const{
if (units_sold){
return revenue / units_sold;
} else {
return ;
}
}
Define a funcion to return "this" object
Sales_data & Sales_data::combine(const Sales_data & rhs){
units_sold += rhs.units_sold;
return *this; // return current object
}
To return an lvalue, our combine function must return a reference.
Member functions that are part of the interface of a class should be declared in the same header as the class itself.
istream & read(istream & is, Sales_data & item){
is >> itme.bookNo >> item.units_sold;
return is;
}
ostream & print(ostream & os, const Sales_data & item){
os << item.isBN();
return os;
}
Both read an writer take a reference to thier respective IO class types. The IO class cannot be copied, so only passed by reference.
Reading or Writing to a stream changes that stream, so both functions take ordinary reference, not references to const.
Sales_data add(const Sales_data & lhs, const Sales_data & rhs){
Sales_data sum = lhs; // copy data members from lhs into sum. statement[1]
sum.combine(rhs);
return sum;
}
We initialize sum as a copy of lhs. By default, copying a class object copies that object's members.
In Java, statement[1] only assign the reference of lhs to sum. Both sum and lhs points to the same object in heap storage.
In C++, statement[1] copy the all member from lhs to sum. lhs and sum points to different object, and are independent.
The compiler generate a default constructor automatically only if a class declare no constructors.
Many class that need dynamic memory can (and generally should) use a vector or a string to manage the necessary storage. Classes that use vectors and string avoid the complexites of allocating and deallocating memory.
A class may contain zero or more access specifier(e.g. private, public). Each access specifier specify the access level of the succeeding members. The specified access level remain in effect until the next access specifier or the end of the class body.
The only difference between struct and class is the default access level.
- In struct, the members defined before the first accesss specifier are public.
- In class, these members are private.
Friend
A class can allow class or function to access its non-public members by marking that as a frined.
class Sales_data{
friend Sales_data add(const sales_data&, const Sales_data&);
public:
Sales_data() default;
private:
string bookNo;
};
// declarations for members which are part of the Sales_data interface
Sales_data add(const Sales_data&, const Sales.data &);
It is good idea to group friend declaration together at the beginning or end of the class. To make a friend visible to the users of the class, we usually declare each friend(outside the class) in the same header as the class.
Encapsulation provide two important advantage:
- User code cannot corrupt the state of an encapsulated object
- The implementation of an encapsulated class can change over time without requiring changes in user-level code.
Although user code need not change when a class definition changes, the source files that use a class must be re-compiled if the class changes.
Class often have small functions that can benefit from being inline.
Mutable
A mutable data member are never const, even when it is a member of a const class
class Screen{
public:
void some_member() const;
private:
mutable size_t access_ctr; // may change even in a const object
};
void Screen::some_member() const{
access_ctr++;
};
In-class initializer
When we provide an in-class initializer, we must do so following on = asign or inside braces.
class windows_mgr{
private:
// By default, a windows_mgr has one blank screen
std::vector<Screen> screens{screen(, , ' ')};
}
Return *this
class Screen{
public:
Screen& set(char);
Screen& move(int i, int j);
// other members. e.g. cursor
};
inline Screen& Screen::move(int i, int j){
cursor = i * width + j;
return *this; // return this object as lvalue
}
inline Screen& Screen::set(char c){
contents[cursor] = c;
return *this;
}
If we concatenate a sequence of these actions into a single expression
myScreen.move(, ).set('#');
Above single expression is equal to
myScreen.move(, );
myScreen.set('#');
Had we defined move and set to return Screen, rather thane Screen &. This statement will be vary different.
Screen tmp = myScreen.move(, ); // the return value would be copied
tmp.set('#'); // the content inside myScreen will be unchanged.
A class is not defined until its class body is completed. A class cannot have data members of its own type.
However, a class is considered declared(not yet defined) as soon as its calss name has been seen. Therefore, a class can have data members that are pointers or reference to its own type.
class Link_Sales{
Screen window;
Link_Sales *next;
Link_Sales *prev;
};
class Screen{
// Windows_mgr can access the non-public part of class Screen.
friend class Windows_mgr;
}
void Windows_mgr::clear(int i){
Screen & s = screens[i];
s.content = '';
}
It is important to understand that friendship is not transitive. If class Windows_mgr has its own friends, that friends have no special access to Screen.
Each class controls which class or function are its friends.
Although overload functions share a common name, they are still different functions. Therefore a class must define as a friend each function in a set of overload functions that it wish to mark as friend.
The lookup rules for friends
Struct X{
friend void f();
X() { f(); } // error: no declaration for f()
void g();
void h();
}
void X::g() { return f(); } // error: f has not been declared
void f(); // declares the function defined in X()
void X:h() { return f(); } // ok
Remember, some compiler do not enforce the lookup rules for friends.
class Window_mgr{
public:
screenIndex addScreen( const Screen&);
// other members
};
// return type is seen before the scope of Window_mgr
Window_mgr::ScreenIndex Window_mgr::addScreen(const Screen & s){
screens.push_back(s);
return screens.size() - ;
}
To use screenIndex for the return type, we must specified.
Member function definitions are processed after all of the declaration in class are processed by compiler. So member functions can use any member declared inside the calss
Even though the class member(height) is hidden, it is accessable by qualified the member's name with the class's name or with this pointer expectly
void Screen::dummy_fcn(pos height){
cursor = width * this->height;
// alternative way
cursor = width * Screen::height;
}
The global object is accessable by the scope opertion.
void Screen::dummy_fcn(pos height){
cursor = width * ::height; // the global height
}
Reference:
C++ Primer, Fifth Edition, chapter 7 Classes
随机推荐
- 关于chrome浏览器不能更新js的问题
今天写程序时,突然发现无论我怎么改本地js,用chrome打开时,均是改动之前的效果,F12查看Sources时发现js文件并没有被改动.由此引发的问题,经查询解决方法如下: F12后按F1,出现Se ...
- http请求常用的状态码
常见的http请求响应的状态码 一些常见的状态码为: 200 – 服务器成功返回网页 404 – 请求的网页不存在 503 – 服务不可用 1xx(临时响应) 表示临时响应并需要请求者继续执行操作的状 ...
- LeetCode 中级 - 救生艇(105)
第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...
- OpenCV-Python 人脸眼睛嘴识别
# 识别眼睛.嘴巴.人脸 image = cv2.imread('./yong.jpg') gray = cv2.cvtColor(image,code=cv2.COLOR_BGR2BGRA) # 加 ...
- openssl windows平台编译库
首先感谢http://blog.csdn.net/YAOJINGKAO/article/details/53041165?locationNum=10&fps=1和https://www.cn ...
- Python学习:9.模块的安装以及调用模块
什么是模块 在Python中,模块其实也就是包含python代码的文件,我们为什么要使用模块?在我们以后写代码的时候,我们会发现有很多功能需要经常使用,那我们想要使用这些功能怎么办,要再把那些代码在敲 ...
- 浅谈fail-fast机制
fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...
- 不使用IDE,用maven命令打包war项目及出现的问题解决(最全攻略,亲测有效)
第一次在博客园写博客,写的不好大家见谅啊!! 一.首先,在用maven打包之前,一定确认好你使用的jdk版本和maven的版本,最好就是用jdk1.8和maven3.3以上的,版本太低了容易出现一些意 ...
- js闭包的理解-目前网上分析的最透彻文章
js的闭包对于大家实际上并不陌生,但是真正敢说自己完全理解的人并不多.笔者在网上看到分析闭包的文章非常多,篇幅用的非常多,但是实际上分析的并不到位,或者根本就是不正确的.我有时候都在想,写这些文章的人 ...
- 【CF613D】Kingdom and its Cities(虚树,动态规划)
[CF613D]Kingdom and its Cities(虚树,动态规划) 题面 洛谷 CF 翻译洛谷上有啦 题解 每次构建虚树,首先特判无解,也就是关键点中存在父子关系. 考虑\(dp\),设\ ...