C++8种常见类类型
大部分面向对象开发工作中都应用了以下部分或者全部的基本类别的类:
1、具体类(concrete class)
2、抽象类(abstract class)
3、接口类(interface class)
4、节点类(node class)
5、支持类(support class)
6、域类(domain class)
7、应用类(utility class)
8、集合和容器类(collection and container class)
反转链表
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pre = nullptr;
ListNode *cur = pHead;
ListNode *nex = nullptr;
while (cur)
{
nex = cur->next;
cur->next = pre;
pre = cur;
cur = nex;
}
return pre;
}
};
3 函数
3.3函数重载
//函数重载曼珠的条件
//同一作用于
//函数名称相同
//函数参数类型不同,或者个数不同,顺序不用
//函数返回值不同不可以作为重载的条件
void func()
{
cout << "func 的调用1" << endl;
}
void func(int a)
{
cout << "func 的调用2" << endl;
}
3.3.2重载注意事项
//函数重载的注意事项
//1.引用作为重载的条件
void fun(int &a)
{
cout << "fun(int &a)" << endl;
}
void fun(const int &a)
{
cout << "fun(const int &a)" << endl;
}
//2.函数重载遇到默认参数
void func2(int a,int b)
{
cout << "func2(int a,int b)" << endl;
}
void func2(int a)
{
cout << "func2(int a)" << endl;
}
//void func2(int a, int b=10) 这个是不对的
int main()
{
int a = 10;
fun(a);
const int b = 11;
fun(b);
fun(20);
func2(10);
}
4类
4.1封装
4.1.1 封装的意义
封装的意义:
1.将属性和行为作为一个整体,表现生活中的事物
2.将属性和行为加以权限控制
class Person
{
protected:
string m_Car;
private:
int m_Password;
public:
string m_Name;
void func()
{
m_Name = "张三";
m_Car = "拖拉机";
m_Password = 123456;
}
};
4.1.2 struct和class区别
4.2 对象的初始化和清理
4.2.3 拷贝构造函数调用时机
使用一个已经创建完毕的对象来初始化一个对象
值传递的方式给函数参数传值
以值方式返回局部对象
4.2.8 静态成员
静态成员就是在成员变量和成员函数前加上static关键字,称为静态成员
静态成员分为:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//vector 容器存放内置数据类型
void print(int p)
{
cout << p << endl;
}
void test01()
{
//创建一个vector容器,数据
vector<int> v;
//向容器中插入数据
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
//通过迭代器访问容器中的数据
vector<int>::iterator itBegin = v.begin();//起始迭代器 指向容器第一个元素的位置
vector<int>::iterator itEnd = v.end();//结束迭代器 指向容器中最后一个元素的下一个位置
//第一种遍历方式
while (itBegin != itEnd)
{
cout << *itBegin << endl;
itBegin++;
}
//第二种遍历方式
for (vector<int>::iterator it=v.begin();it!=v.end();it++)
{
cout << *it << endl;
}
//第三种遍历方式 利用STL遍历算法
for_each(v.begin(), v.end(), print);
}
int main()
{
test01();
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_age = age;
}
string m_Name;
int m_age;
};
void test01()
{
vector<Person> v;
Person p1("a",10);
Person p2("b", 12);
Person p3("c", 13);
Person p4("d", 14);
Person p5("e", 15);
//向容器中添加数据
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
v.push_back(p5);
//遍历容器中的数据
for (vector<Person>::iterator it=v.begin();it!=v.end();it++)
{
cout << (*it).m_age << "空格" << it->m_Name << endl;
}
}
void test02()
{
vector<Person*> v;
Person p1("a", 10);
Person p2("b", 12);
Person p3("c", 13);
Person p4("d", 14);
Person p5("e", 15);
//向容器中添加数据
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
v.push_back(&p4);
v.push_back(&p5);
//遍历容器中的数据
for (vector<Person*>::iterator it = v.begin();it != v.end();it++)
{
cout << (**it).m_age << "空格" << (*it)->m_Name << endl;
}
}
int main()
{
test01();
test02();
return 0;
}