C++学习笔记1

知识点1

#include
#include is to insert the included file into the .cpp file at where the #include statement is.

#include"xx.h":first search in the current directory,then the directories declared somewhere
#include<xx.h>:search in the specified directories
#include:same as #include"xx.h"

.h里面放声明而不放定义。避免多个.cpp去include同一个.h时,Link遇到重复定义的东西
Standard header file structure

#ifndef HEADER_FLAG
#define HEADER_FLAG
//Type declaration here......
#endif //HEADER_FLAG

防止在一个.cpp里面include同一个.h多次,而出现那个.h里面的类的声明被重复出现
Tips for header
1、one class declaration per header file
2、Associated with one source file in the same prefix of file name.
3、The contents of a header file is surrounded with #ifndef #define #endif

知识点2

成员变量
Call functions in a class

Point a;
a.print();

There is a relationship with the function be called and the variable calls it.
The function itself konws it is doing something with the variable.

this:the hidden parameter
this is a hidden parameter for all member functions
调用函数时,所有的成员变量都有一个this指针this->i

To call teh function,you must specify a variable

Point a;
a.print();
Point::print(&a);

知识点3

构造constructor 创建对象时会被调用
How a constructor does?

class X{
	int i;
public:
	X();
};

constructors with arguments
The constructor can have arguments to allow you to specify how an object is created,give it initialization values,and so on.

Tree(int i){...}
Tree t(12);

析构函数The destructor 结束对象时被调用

class Y{
public:
	~Y();
};

知识点4

对象初始化

struct Y{float f;int i;Y(int a);};
Y y1[]={Y(1),Y(2),Y(3)};

The default constructor(默认构造函数)
A default constructor is one that can be called with no arguments.

知识点5

Dynamic memory allocation

new int;
new int[10];
delete p;
delete[] p;  //空间都收回,析构函数只调用了一个

Dynamin Arrays

int *psome = new int [10];

The new operator returns the address of the first element of block

delete  [ ] psome;

The presence of the brackets tells the program that it should free the whole array,not just the element.

知识点5

访问限制 Setting limits

to keep the client programmer’s hands off members they should’t touch.

to allow the library designer to change the internal workings of the structure without worrying about how it will affect the client programmer.

–public

–private
只有在内部函数才能访问(对同一个类来说,同一类对象之间是可以访问私有的变量的)

–friend
声明是朋友后,可以自由访问私有变量
友元
https://blog.csdn.net/txl199106/article/details/44757649

–protected
只有自己和子类能够访问

struct X;
struct Y{
	void f(X*);
}

struct X{
private:
	int i;
public:
	void initialize();
	friend void g(X*,int);
	friend void Y::f(X*);
	friend struct Z;
	friend void h();
};

对于struct 和 class 的区别:
class 没给出属性的默认为private
struct 没给出属性的默认为public

知识点6

初始化列表

class Point{
private:
	const float x,y;
	Point(float xa=0.0,float ya=0.0):y(ya),x(xa){}
};
Student : : Student (string s) : name(s){ }

initialization before constructor

Student : : Student (string s) {name=s;}

assignment

知识点7

对象组合
ways of inclusion

-Fully 别人的对象就是我的对象的一部分

-By reference 可以调用别人的对象

Example

class Person{...};
class Currency{...};
class SavingsAccount{
public:
	SavingsAccount(const char* name,const char* address,int cents);
	~SavingsAccount();
	void print();
private:
	Person m_saver;  //Fully
	Currency m_balance;
}
SavingsAccount::SavingsAccount(const char* name,const char* address,int cents):m_saver(name,address),m_balance(0,cents){}
void SavingsAccount::print(){
	m_saver.print();       //By reference
	m_balance.print();
}

知识点8

继承 Inheritance(Reusing the interface)
对象组合和继承的区别:对象组合是拿对象拼出一个新的对象,继承是拿类拼出一个新的类。
在这里插入图片描述列表初始化 父类的构造函数初始化只能放在列表初始化中

class A{
public:
	A():i(0){ cout<<"A::A()"<<endl;}
	~A() {cout <<"A::~A()" <<endl;}
	void print() {cout<<"A::f()"<<i<<endl;}
	void set(int ii) { i=ii;}
private:
	int i;
};
class B : public A{   //B是A的子类
public:
	B():A(15){};
	void f() {  //列表初始化 父类的构造函数初始化只能放在列表初始化中
		set(20);
		//i=30;   父类私有的成员不能改变  
		print(); 
	}
	void print(){cout<<"B::print()"<<endl;}  //如果子类中有和父类同名的函数,父类的函数被隐藏
};

知识点9

函数重载 overloading(函数名,返回类型相同,参数不同)
Same functions with different arguments list.

默认参数 Default arguments(一般不使用)
可以预先给函数赋值

Stash(int size , int initQuantity = 0);

知识点10

内联函数 Inline Functions(避免调用函数的麻烦操作)
以空间换时间

inline int plusOne(int x);
inline int plusOne(int x) { return ++x; };

Repeat inline keyword at declaration and definition.

You can put inline fuctions’ bodies in header file. Then #include it where the function is needed.
Never be afraid of multi-definition of inline functions,since they have no body at all.
Definitions of inline functions are just declarations.

#define f(a) (a)+(a)           //不会检查出类型错误
main(){
	double a=4;
	printf("%d",f(a));
}
inline int f(int i){
	return i*2;
}
main(){   //报错
	double a=4;
	printf("%d",f(a));
}

Inline:
Small functions,2~3 lines
Frequently called functions.e.g. inside loops

Not inline?
Very large functions,more than 20 lines
Recursive functions

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值