【COMP282 LEC 1-2】

LEC 1 Introduction to c++

Header file :

1. 不用写 " .h "

Function : 

数据类型有string了,就像java,需要#include

Output system :

Explain by example:

std::cout << hello << " is of length " << x;

std::cout means standard character output

<< means insertion (so we insert into std::cout) – each of them inserts into the left most

  It must be by types supporting <<

all standard types does support << and we will later in the course see how to add this feature in our own classes

The rest are strings or numbers We can insert as many as we would like

It looks better than printf, because we do not need to have that format string first

Namespace

std::bla says that bla is in the standard namespace

Namespace is like packages in java

E.g. std::cout means cout as it is defined in the std namespace and std::endl means endl as it is defined in the std namespace

Input system : 

Input is done by writing std::cin >> bla;

  When we have a std::string called bla

  It waits until a line has been written and insert the part upto the first space in bla

LEC 2

C++ classes

>> Classes in C++ is an option – not a requirement

>> If you do not like classes, you can simply not write any in C++

  You can still get things like nicer strings and/or nicer input in/output

  Some small programs are written like that…

  Take the features you like, avoid the features you do not…

【COMP282 LEC 1-2】_第1张图片

就像上面那张图一样,class的后括号要加一个分号,这一点不太一样

 Access modifiers in C++

>> There are three access modifiers in C++ : private(default), public, protected

>> Most is the same as Java

>>The syntax is different from Java: You write e.g. public: and then a list of declarations and they are all public 不是写在类的那一行的前面,而是在括号里面

Example : 

【COMP282 LEC 1-2】_第2张图片

Getters and Setters 

Generally we don’t want to expose internal variables to the outside world, so we keep them private and define getters and setters

   >> We do not actually want to expose getters and setters either, except for cases (like here), where we are using the class directly for data storage

【COMP282 LEC 1-2】_第3张图片

Constructors and Destructors

Constructors in C++ behave the same as they do in Java

  >> Called whenever an object is created  

  >> Has no return type and must have the same name as the class

  >> Performs any setup required by the class (such as memory allocation) 

  >> If you don’t declare a constructor then a default one will be used

  >> If you do declare one, the default one will get removed

Java does not have destructors because it uses garbage collection

  >> Objects are automatically deleted when no longer in use

  >> Memory is freed and tidied up automatically

In C++ the destructor is called whenever an object is deleted

  >> Has the same name as the class with a tilde in front of it

  >> Should free up any memory and resources allocated by the constructor 应该释放构造函数分配的内存和资源

Memory Management in C++

【COMP282 LEC 1-2】_第4张图片

Member Initialisation

【COMP282 LEC 1-2】_第5张图片

Inheritence

Like in Java, but with different syntax

Child classes inherit variables and methods of their parent

Difference: There are no interfaces though 没有接口

Difference: C++ has access modifiers on inheritance  访问修饰符

【COMP282 LEC 1-2】_第6张图片 

Polymorphism(多态性):【COMP282 LEC 1-2】_第7张图片

首先dog类继承了父类animal类,他继承了父类的方法“makeNoise”。在main里,创建了一个dog类叫“rover”

?????

Virtual Polymorphism: 

【COMP282 LEC 1-2】_第8张图片

Remember that C++ is used for coding low-level device drivers, embedded systems, etc.

1. Needs to generate small, memory efficient, fast executable code

2. Polymorphism adds complexity and size to the finished product

3. So, the compiler only does it when we explicitly ask for it

Initialisation:

 

【COMP282 LEC 1-2】_第9张图片

【COMP282 LEC 1-2】_第10张图片 

Slicing:分割

【COMP282 LEC 1-2】_第11张图片

1. 定义了一个方法:“output” 作用就是打印出一个格式,比如说这里就是

Fluffy weighs 5 and has lives 9

2. 把子类的data(fluffy) pass to 了父类animal(pet)

【COMP282 LEC 1-2】_第12张图片

Accessing child through parent: 【COMP282 LEC 1-2】_第13张图片

The three ways to pass objects to functions: 

【COMP282 LEC 1-2】_第14张图片 

结果:

rover weighs 8

fluffy weighs 0

Permanent objects:​​​​​​​ 【COMP282 LEC 1-2】_第15张图片

 

你可能感兴趣的:(c++,开发语言)