C++的基础知识
- 1、前言
- 2、引用
-
- 3、封装
- 4、C++的类
- 4.1 什么是类和对象
- 4.2 类的访问限制
- 4.3 类的继承
- 4.3.1 public继承
- 4.3.2 protected继承
- 4.3.3 private继承
- 5、多态
-
- 6、函数重载
- 7、总结
1、前言
记录一下C++的一些基础知识,方便自己日后回顾,也可以给有需要的人提供帮助。 |
2、引用
2.1 引用的概念
int main()
{int a = 1;int &b = a; cout << b << endl;cout << a << endl;return 0;
}
2.2 引用与指针的区别
1. 引用无空值(不能为NULL),指针可以;
2. 引用初始化后不可更改,指针可重新指向;
3. 支持多级指针,不支持多级引用;
4. 无需解引用操作符。 |
3、封装
封装就是将数据(成员变量)和操作数据的方法(成员函数)绑定在一起,并隐藏
(private)内部实现细节,重新提供外部访问接口进行访问。 |
#include <iostream>using namespace std;
class BankAccount{
private:string owner; double balance = 0.0;
public:BankAccount() : balance(0.0) {}void setOwner(const string& o){owner = o;cout << "重置用户名成功,当前用户名为:" << o << endl;}void deposit(double amount){balance += amount;cout << "存款成功,当前余额为:" << balance << endl;}};
int main()
{BankAccount ba;ba.setOwner("叶子");ba.deposit(10.21);return 0;
}

4、C++的类
4.1 什么是类和对象
类是创造对象的模板,对象是类的定义出来的变量;
类的成员不但可以是变量,还可以是函数;
一个类可以创建多个对象,每个对象都是类类型的一个变量;创建对象的过程叫类的实例化。 |
class Dog{
public:string name;int age;void run(){cout << "小狗会跑" << endl;};
protected:double weight;
private:string color;
};
int main()
{Dog dog1;Dog *dog2 = new Dog();return 0;
}
4.2 类的访问限制
访问权限 | 特点 |
---|
public | 成员无访问限制 |
protected | 成员在派生类中可访问 |
private | 成员只能在类内部访问 |
class Dog{
public:string name;int age;void run(){cout << "小狗会跑" << endl;};void setColor(const string &c){color=c;}
protected:double weight;
private:string color;
};
class xiaohei:public Dog{void setWeight(){weight = 20; }void setColor(){};
};
int main()
{Dog dog1;dog1.name = "小黑"; Dog *dog2 = new Dog();dog2->setColor("黑色"); return 0;
}
4.3 类的继承
继承方式 | 基类public成员 | 基类protected成员 | 基类private成员 | 外部代码 |
---|
public | 在派生类中仍为 public | 在派生类中为 protected | 不可访问 | 只能访问派生类的 public 成员 |
protected | 在派生类中变为 protected | 在派生类中仍为 protected | 不可访问 | 无法直接访问基类成员 |
private | 在派生类中变为 private | 在派生类中变为 protected | 不可访问 | 无法直接访问基类成员 |
4.3.1 public继承
class Animal{
public:int age;void setAge();
protected:double weight;void setWeight();
private:string color;void setcolor();
};
class Dog : public Animal{
public:void setPropetry(){age = 10; weight = 20; setAge(); setWeight(); }
protected:void setPropetry2(){age = 100;weight = 200;}};int main()
{Dog dog1;dog1.age;return 0;
4.3.2 protected继承
class Cat : protected Animal{
public:void setPropetry(){age = 2; weight = 100; setAge(); setWeight(); }
};int main()
{Dog dog1;dog1.age;Cat cat;return 0;
}
4.3.3 private继承
class Duck : private Animal{
public:void setPropetry(){age = 3; weight = 30; setAge(); }
};int main()
{Dog dog1;dog1.age;Cat cat;return 0;Duck duck;
}
5、多态
5.1 多态的实现
1. 基类定义虚函数(virtual 关键字)
2. 派生类重写虚函数(override 关键字)
3. 通过基类指针或引用调用虚函数,触发动态绑定 |
#include <iostream>using namespace std;class Animal{
public:virtual void speek(){cout << "Animal sound" << endl;};virtual ~Animal() {}
};class Dog : public Animal{
public:void speek() override{cout << "wang" << endl;}
};
class Cat : public Animal {
public:void speek() override{cout << "mi" << endl;}};
int main()
{Animal *animal1 = new Dog();Animal *animal2 = new Cat();animal1->speek(); animal2->speek(); delete animal1;delete animal2;return 0;
}
6、函数重载
函数重载就是函数名称相同,但这些函数的参数列表不同(参数类型、参数个数或参数顺序不同) |
int add(int a, int b){return a+b;
}int add(int a, int b, int c){return a+b+c;
}double add(double a, double b){return a+b;
}int main()
{cout << add(1,2) << endl; cout << add(2,3,4) << endl; cout << add(1.5, 1.3) << endl; return 0;
}
7、总结
以上就是C++的一些基础知识了,浏览过程中,如若发现错误,欢迎大家指正,有问题
的欢迎评论区留言或者私信。最后,如果大家觉得有所帮助,可以点一下赞,谢谢大
家!祝大家天天开心,顺遂无虞! |