一.类的定义(与struct相似)
#include <iostream>
using namespace std;
class Point{//定义类
public:
int x;
int y;
int z;
void Print(){// void Print(Point* this) 下式中的 this-> 可省略
cout << "(" << this->x << "," << this->y << "," << this->z << ")" << endl;
}
};
int main(){
Point p;
p.x = 1;
p.y = 2;
p.z = 3;
p.Print();//调用p类里的Print函数
}
- 默认为private,所以需要设定public
- 对象做参数和返回值
Complex Add(Complex c1,Complex c2);
Complex Complex::Add(Complex c);
- 构成: 成员变量(只能在类定义中声明,可以在成员函数中直接调用),成员函数(可以在类内实现或类外实现)
例:账单
#include <iostream>
#include <cstdlib>
using namespace std;
class Bill{
public:
char name[20];
int count;
float price;
float GetTotal(){
return count*price;
}//折扣
};
class Account{
public:
Bill* bills;//有很多个账单,不能用数组
size_t count;
void Init(){//初始化
bills = NULL;
count = 0;
}
void Add(Bill bill){//将初始化的bill放入bills指针中
++count;
bills = (Bill*)realloc(bills,count*sizeof(Bill));
bills[count-1] = bill;
}
void Print(){
cout << "name\tcount\tprice\ttotal" << endl;
cout << "-----------------------------" << endl;
float sum = 0;
for(int i=0;i<count;++i){
cout << bills[i].name << "\t";
cout << bills[i].count << "\t";
cout << bills[i].price << "\t";
cout << bills[i].GetTotal() << endl;
sum += bills[i].GetTotal();
}
cout << "-----------------------------" << endl;
cout <<"Total:\t\t\t" << sum << endl;//"\t"是tab键,使对齐
}
void Destory(){//摧毁
free(bills);
bills = NULL;
count = 0;
}
};
int main(){
Account account;
account.Init();
Bill bills[] = {
{"Apple",10,2.5},
{"Orange",5,3},
{"Pair",4,3.6}
};
for(int i=0;i<3;i++){
account.Add(bills[i]);
}
account.Print();
account.Destory();
}
二.vector
向量(vector)是一个能够存放任意类型的动态数组。
1.创建
vector<int> a; // 创建一个空类型为int的vector
vector<float> b(10); // 创建一个有10个元素类型为float值为0的vector
vector<char> c(20,'a'); // 创建一个有20个元素类型为char值为'a'的vector
2.获取元素个数
获取元素个数对象.size()
判读是否为空对象.empty()
3.添加/删除元素
尾添加对象.push_back()
尾删除对象.pop_back()
清空数组对象.clear()
4.访问元素
获取第一个元素对象.front()
获取最后一个元素对象.back()
随机访问对象[下表]
或者对象.at(下标)
5.运算符
赋值=
比较==
例:交换两组数据
#include<iostream>
using namespace std;
int main(){
//作交换(数组做不到的):
vector<int> a = {1,2,3,4,5}; // C++11
vector<int> b = {6,7,8,9,10};
vector<int> t = a;
a = b;
b = t;
for(int i=0;i<a.size();++i){
cout << a[i] << " ";
}
cout << endl;
}
三.string
字符串(string)是一个能够方便操作的动态字符数组。
- 获取字符个数
获取字符个数对象.size()
/对象.length()
判读是否为空对象.empty()
- 添加/删除
尾添加字符对象.push_back()
尾删除字符对象.pop_back()
尾添加字符串对象.append()
- 访问字符
获取第一个元素对象.front()
获取最后一个元素对象.back()
随机访问对象[下表]
或者对象.at(下标)
获取字符数组对象.c_str()
- 运算符
赋值=
比较==
添加+
输入>>
输出<<
例:
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cin >> s;//不限制输入大小,例:dfhusfhsg
cout << s << endl;
cout << s.size() << endl;//可以得出string的大小
//也可以整体遍历:
for(int i=0;i<s.size();++i){
out << s[i] << " ";
}
cout << endl;
//也可以跟其它字符串进行连接
s.append("12345");
//or: s = s + "12345";
cout << s << endl;
//也可以做整体赋值
s = "abc";
cout << s << endl;
}
四.class与struct的区别
1.默认的访问控制不同
struct是public,class是private。
2.strcut可以使用花括号内的初始值列表{...}初始化,class不可以(C++98不可以,C++11可以)
五.对象创建
1.直接创建--栈上创建
基本语法:
类名 对象名; // 调用默认构造函数
类名(); // 创建匿名对象
2.动态创建--堆上创建
基本语法:
类名* 对象指针 = new 类名;// 调用默认构造函数
delete 对象指针;
对象指针new可以为对象设置初始值
int* p = new int(100);
cout << *p << endl;
类类型:
// 定义类
class Demo{};
// 创建对象
int main(){
Demo* d = new Demo;
delete d;
d = NULL;
}
3.动态创建数组--堆上创建
int* pa = new int[10];
delete pa;// 只释放p[0]
delete [] pa;// 释放全部数组
类类型:
// 定义类
class Demo{};
// 创建对象
int main(){
Demo* d = new Demo[10];
delete [] d;
d = NULL;
}
对象数组指针new
不可以为对象设置初始值。
注:空结构体与空类的大小(sizeof
)为1
,主要在于初始化/实例化时,编译器给变量/对象分配内存(地址),内存最小单位为1个字节。
通常,sizeof(类型) == sizeof(变量)
。
六.this指针
- 当一个对象被创建时,
this
指针就存放指向对象数据的首地址 - 不是对象本身的一部分,不会影响
sizeof(对象)
的结果#include<iostream> using namespace std; class Simple{ int n;//成员变量 // int m_n; 更好地代表成员变量(Micosoft C++) // int _n; (Google C++) public: void SetN(int n){ this->n = n; } void Print(){ cout << n << endl; } }; int main(){ Simple s; s.SetN(10); s.Print(); }