1. 不用写 " .h "
数据类型有string了,就像java,需要#include
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
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 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
>> 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…
就像上面那张图一样,class的后括号要加一个分号,这一点不太一样
>> 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 :
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
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++
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 访问修饰符
首先dog类继承了父类animal类,他继承了父类的方法“makeNoise”。在main里,创建了一个dog类叫“rover”
?????
Virtual Polymorphism:
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
1. 定义了一个方法:“output” 作用就是打印出一个格式,比如说这里就是
Fluffy weighs 5 and has lives 9
2. 把子类的data(fluffy) pass to 了父类animal(pet)
结果:
rover weighs 8
fluffy weighs 0