Section 3


原文档地址: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 

1697421374693-ee648ef2-f4a9-4da9-b3ea-038756ce3c3d.png

Your First Class

With r and h, a cylinder is modeled

A ,V can be computed.

1697421559852-aa3b1004-21a4-426a-a03f-4b0a2a991dc4.png

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

 

1697421636009-e6e67072-2382-4fcc-b380-226c82cfd359.png

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:

1697423065023-88a0f507-054d-4655-b171-8b8765c53087.png

1697423071476-79fa7dc9-ffef-4051-b6c4-5674524f8d70.png

1697423096041-7a9baaa1-b52d-4894-b074-200c232dd8d2.png

Manage class Objects with pointers

1697458998465-345cc9cc-ed65-49d7-8f20-4c65b617d47e.png

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

1697460276290-a6567405-ca36-4e99-a83f-d2d1ee8949ef.png

ex:

1697459112322-c69088f1-7b78-48a2-a30b-879f3cf0ed31.png

1697459105904-07572d93-55ae-45e2-8442-70bfd563b325.png

1697459192566-4413ec2e-bd3e-4055-9988-2fddb489f04f.png

1697459266295-338a62ec-f6ad-4cc9-a236-92f20f7df8e8.png1697459940022-2ee34311-9768-4dbc-a2b9-8eb5af5063a7.png

1697460294165-c827119c-8f30-45e4-aacb-cf1ad11b22b0.png

1697460276290-a6567405-ca36-4e99-a83f-d2d1ee8949ef.png

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

1697424264237-4fe85512-0942-44d3-b28e-3df628dbd54c.png

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

1697426386072-23f398fd-0a0e-47c5-b727-5c0cf561e0e3.png

1697421636009-e6e67072-2382-4fcc-b380-226c82cfd359.png

1697423065023-88a0f507-054d-4655-b171-8b8765c53087.png?x-oss-process=image%2Fresize%2Cw_329%2Climit_0

1697426433354-92736a1e-f018-4cb2-a050-be15762ed89b.png

ex2:

constructor with no para

object use constructor 

( constructor construct the object )

1697425286664-10d73d43-6a4b-4e60-8b97-9d92b64ec6b8.png1697425385938-2f63408b-46e3-4ff5-a7c5-c9b195a747eb.png

1697425341643-0cd1832b-117b-4514-b8d2-ee79f631248f.png

1697425308462-2e7734c8-ee68-4c1b-a767-44369ce73ef5.png

constructor take parameters 

1697426644400-c7c286fc-4d37-4115-bb02-73afbd2c51ff.png

1697425385938-2f63408b-46e3-4ff5-a7c5-c9b195a747eb.png

1697425994438-af343240-b9f0-4f75-9446-426f47643017.png

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

1697444286262-3575ebbb-5e80-44ff-bf77-c1de6654651a.png

1697444359099-8da9fc8a-545b-47ea-b601-65d3d486cc50.png

1697444367377-5af8f773-f27a-482a-86c7-d152e382cb9d.png

 

ex2:

1697426687492-c17ebdc4-bd12-48ad-8a16-a087930d401b.png

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

 

1697444588746-dd50b379-641f-4192-9829-c8a7d4b72310.png

1697446114041-b900af6f-2d01-4470-8dfb-23df0cb72607.png

1697446118922-8918b228-44b3-4918-81b8-2af1a1ffaee8.png

1697446155308-40acd197-cec2-4c41-a50d-2eb27c52e74b.png

1697446211702-a28991c9-3aea-4e8f-82cc-f524eb6b86b2.png

 

Destructors

special function(methods) in a class called by compiler to destory object

constructor for allocating memory

deconstructor for destory memory

1697460409127-0d6a52f9-d818-4526-8151-e775bed9437e.png

1697460946849-63b17f9d-2211-4fc9-85ee-b9967c1112d6.png

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 可以用于访问字符串的字符数据,而不需要分配新的内存或复制原始字符串的内容。它提供了字符串的开始地址和长度,因此它不拥有字符串数据,只是一个字符串的引用或视图

1697462085056-9890f359-92b4-435f-b916-1b9355f7b143.png

deconstructor for destoring memory1697462219977-80303643-76b5-49d8-94c5-65944697e0ad.png

1697462341530-9511941b-d73d-4e85-bb2c-4d61d42c4759.png

1697462416345-73161e77-5323-4fb5-8358-b2d2818a8ab2.png

Order of Constructor/Destructor Calls

1697531807097-4cb9ad0c-cdbf-4974-87f2-cda5b20a1dbd.png

1697531792153-5711a0fd-6617-4acb-9743-1223fbd95973.png

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.

1697533403578-32093a4d-fe13-4477-bb85-af653441a053.png

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

1697533733897-1b4bab27-52a5-498c-9394-485267cf6098.png

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

1697592645008-4ce81d32-15ef-4825-a3b6-4821401c7f4c.png

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 

1697593309155-e49d22cf-1d76-43bb-90ea-08a9e4795ff7.png

just return the address of member varibale at the end of all function using "this" pointer

ex:

1697593819980-44bed5f0-d9f5-4451-9a49-2b2de98bd59c.png?x-oss-process=image%2Fresize%2Cw_473%2Climit_0

1697593469516-fc61859e-082b-4f09-aea9-8b251fe82af1.png

a chain of function calls using reference to achive 

1697594239349-fb66e70a-8e59-46a2-8d9e-2086cf131173.png

1697594228705-3e7f97cb-1005-42b8-a9b5-f03661d60c80.png

Class Across Multiple Files

1697446345518-5ff9a1b2-a27e-47bd-8caf-148a5e610613.png

1697446356369-cd57b748-ce88-4834-8a86-8fcf9df4ea77.png

1697446413788-3edac8a8-7486-4d9d-a1ec-316d3e58073f.png

1697446450723-c79a061b-fa1f-4937-a0ff-609bc26f77be.png

ex1

1697446790061-f5233a7d-cc83-4727-8750-fa0e0e022420.png

to avoid twice definition on cylinder.h and man.cpp

it is a guard gurading constrants.h including more than once1697447696261-9633c3ae-7aa8-4202-8f99-c5703fa68cbe.png

1697446945200-044c8208-e008-4e07-929a-970552ef6417.png

it is a guard gurading cylinder.h including more than once

1697447479120-a0b46c25-b946-4d77-a1f4-c49c730126f2.png in cylinder.h

1697444588746-dd50b379-641f-4192-9829-c8a7d4b72310.png?x-oss-process=image%2Fresize%2Cw_515%2Climit_0

1697447495203-57a6438c-14e0-4ebd-874e-2c994b921937.png

 

 

1697447572198-c7b2c5e3-5a48-44b1-8aa3-48620d73b3e6.png

1697447567203-d63022b8-86bb-4894-adf7-09b67a34d749.png

ex2:

1697446790061-f5233a7d-cc83-4727-8750-fa0e0e022420.png

to avoid twice definition on cylinder.h and man.cpp

it is a guard gurading constrants.h including more than once1697447696261-9633c3ae-7aa8-4202-8f99-c5703fa68cbe.png

 

 

1697446945200-044c8208-e008-4e07-929a-970552ef6417.png -----header

it is a guard gurading cylinder.h including more than once

1697447479120-a0b46c25-b946-4d77-a1f4-c49c730126f2.png1697448023752-9239db94-b8c7-4758-80b0-b8e1aea6f50e.png

1697448271452-102af41f-5a34-4758-a890-a4808f4cab57.png

1697448456935-60f1ebe1-283a-4af6-a32c-942d02da0d51.png

1697447495203-57a6438c-14e0-4ebd-874e-2c994b921937.png

 

1697447933532-ab57c500-3580-4cdf-b889-b17df9d24fc7.png implementation file ------functions of class

class_name:: -------scope resolution operator

tell function that itself live in the scope of class1697448072700-07c0def1-6f2e-4588-8e58-d3a251833ac8.png

1697448431166-3300e095-1584-44ec-a90c-2488889ce7aa.png

1697447572198-c7b2c5e3-5a48-44b1-8aa3-48620d73b3e6.png

1697447567203-d63022b8-86bb-4894-adf7-09b67a34d749.png

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

1697594336156-2060e16d-dc3e-4963-a98e-d537715564e3.png

1697594808714-0e1994f8-c875-47f7-9e4f-8c11a92f79be.png

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:

1697595266122-ce346f65-c671-4907-8b03-f25d027736ae.png----four bytes

1697595302149-5d0b3bf1-e2f9-436f-92d7-4162e92fc368.png----four bytes

1697595311880-4dcd762f-ac60-444a-92a2-492ebb2e35ed.png---- 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的大小会因实现而异,但通常它包括一个指针、字符串长度和其他管理数据,因此会占用相当多的内存

1697594979453-2cb1d5a2-2f20-4060-9425-b054b315d4e7.png

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

1697601014326-21ad82bb-b4f2-4cb3-9caa-9fc9f9142a6f.png

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值