原文档地址:https://www.yuque.com/yuqueyonghubpajeq/wdr7hn/ld596vggsaehg9ze
Class
Mechanism:
1.build own variable types ( like int ,double,,etc ) by setting up blueprints/template of class type
2.use blueprints/template to bulid actual objects
Having a class as blueprint and create real class objects in our c++ program
Your First Class
With r and h, a cylinder is modeled
A ,V can be computed.
Declare class including member variable/methods
The first public----- describe the properties of cylinder(member variables)
The second public -------describe the behavior and functions(methods) that do thing on the class using properties
pubilc---------the members in this class are accessible from the out of the class
privite---------the properties and funtion of class can't be access out of the class
Use a class through class objects
在面向对象编程中,对象是根据类(class)定义创建的实例(instance)。
类是一种抽象的数据类型,它描述了对象的属性(成员变量)和行为(方法或成员函数)。
对象是类的实例化,也就是根据类的定义创建的具体实体,具有类中定义的属性和行为。
类是对象的模板,而对象是类的具体实例。
Declare class object (a variable of class)
Format: class_name object_name;
class_name is a blueprint, we create object of the class (class object or variable_name) from that blueprint.
Use class object (a variable of class)
1.object_name . member_variable = certain_value
2.object_name . class_function( )
ex:
Manage class Objects with pointers
declare a pointer of objects
syntax1: stack object
class_name * pointer_name = & object_name;
syntax2: heap object --- "new" will allocate memory on the heap
class_name* pointer_name = new class_name(paras of object);
use a pointer of objects
syntax1:
* pointer_name.methods(paras of method);
syntax2:
pointer_name->method(paras of method);
注: -> --- called arrow pointer call notation(箭头指针调用表示法)
delete a pointer of object
ex:
Constructor
is a special class methods of functions that called by the compiler to construct your class object
Constructors are functions in class --- can be called by compiler to initilize(modify of construct) the properties of class objects
1.constructor have no return type
2.can have parameters
Declare a construtor inside class
format:
class_name(parameters){
initilize(modify of construct) the properties of class objects
}
Use a construtor when declare a class object
Format:
class_name object_name(parameter to pass in constructor)
ex1:
no construct
the compiler will set up a empty consturctor
ex2:
constructor with no para
object use constructor
( constructor construct the object )
constructor take parameters
Default constructor
class_name( )=default;
equal to:
class_name( ){
};
ex:
if we only set up a construtor with parameters,but declare a class object without passing para value
it will geta compile error
because there is no construtor without parametes
the compiler will not set up a empty constructor with no parameters(default constructor) when there is a constructor with parameters
ex2:
Setters and Getters
Setters and Getters must be public to be accessiable from outside
Setters and Getters can have return type
Getter
if member variables are private,Getters are methods(functions) in class used to read member variab les
syntax:
variable_type getter_name( ){
return memebr variable;
}
Setter
Setters are methods(functions) in class used to modify data in our member variables
syntax:
void setter_name(input para){
memebr variable= input para;
}
1.setter don't need output something,so use void
2.constructor is used to intialize data in our member variables
Destructors
special function(methods) in a class called by compiler to destory object
constructor for allocating memory
deconstructor for destory memory
syntax:
~class_name (){
do whatever is unique to destory memroy variables:
}
注:1.destructor have no paras in ()
2.destructor will execute when the main function is about to end?????
3.can't delete one adress twice
4. the copy(reference ) of pointer is???? (which is confusing)
ex:
methods in class allocate and destory memory
constructor for allocating memory
td::string_view 可以用于访问字符串的字符数据,而不需要分配新的内存或复制原始字符串的内容。它提供了字符串的开始地址和长度,因此它不拥有字符串数据,只是一个字符串的引用或视图
deconstructor for destoring memory
Order of Constructor/Destructor Calls
the destructor wil be called in the reverse order
The this Pointer
special pointer that maintain by c++ that help to manipulate the current object
and it is the current object for which a given method(function) is executed.
use case1: print out object address in a funtion
1.we may print address or memory where the object is created (do that inside of the function )
use "this" key word to get the address of the current object for which a specific method(function) is called
2. in this case ,the specific method is the constructor
"this " is a special pointer that is maintain by c++ system
and it can point to the current object which is manipulated by c++ runtime through a special function of the class object that is being called
3.use "this" to print out the dog_name address in constructor
use case2:
it is used to resolve confilts when you have parameter and member varibale that are named same way
format:
this->variable_name --------refer to the member variable that is name in the same way with other variables
use case3: a chain of function calls using pointer to achive
if we want to set up a chain of function calls using pointer to achive
just return the address of member varibale at the end of all function using "this" pointer
ex:
a chain of function calls using reference to achive
Class Across Multiple Files
ex1
to avoid twice definition on cylinder.h and man.cpp
it is a guard gurading constrants.h including more than once
it is a guard gurading cylinder.h including more than once
in cylinder.h
ex2:
to avoid twice definition on cylinder.h and man.cpp
it is a guard gurading constrants.h including more than once
-----header
it is a guard gurading cylinder.h including more than once
implementation file ------functions of class
class_name:: -------scope resolution operator
tell function that itself live in the scope of class
Struct
在C++中,类的对象和结构体的成员(或属性)之间没有本质的区别,因为在C++中,结构体可以被视为一种特殊的类。结构体和类都可以拥有成员变量和成员函数,并且它们的使用方式和访问方式是相同的。
The difference:
1.class member variable and method are private by default
struct member variable and method are public by default
2.change the default by putting privater or public inside class
Size of objects
Size of object = Total size of all member variables
function are affiliated with class itself
function are not affiliated with class objects
the class is the blueprint from which we are going to generate thousands of object
functions is associated to the blueprint itself
ex:
----four bytes
----four bytes
---- a string is internally implemented as a class and it stores its data as const char pointers(std::string 内部包含指向字符串数据的指针、字符串长度和其他管理数据,因此它的大小不仅仅是字符串本身的大小)
(std::string 类的大小是根据字符串中包含的字符数来确定的,在大多数系统中,一个字符通常占用 1 字节)
the size of an object doesn"t cout the things you are referring to using pointers
the size of an object cout the size of pointer but don't cout the thing the poiner point to.
将返回MyClass类的大小,包括std::string 对象的内部数据和对齐所需的填充字节。需要注意的是,std::string的大小会因实现而异,但通常它包括一个指针、字符串长度和其他管理数据,因此会占用相当多的内存
Boundary alignment:
compiler do when the member varibale of class have different types
the member variable that have 4 bytes will be stored at memory location that are multiples of four
because of this phenomenon,we might have gaps between oue memory
ex:
if we have variables that have 2 byte and4 byte ,it will have gaps inside
and the size of the object will be slightly off the sum we do for the sizes of our object
即内存对齐(alignment):编译器通常会要求数据成员按照某种规则对齐,以提高访问速度和性能。对齐规则通常是数据成员的大小或者平台特定的。比如,一个4字节的整数可能需要按照4字节对齐,而一个8字节的双精度浮点数可能需要按照8字节对齐。这可能会导致在数据成员之间插入填充字节,以确保对齐要求。
Introduction to Inheritance
a feature that c++ provide to allow bulid class in terms of other class
we can build a complete hierarchies of class that deoend on other class