封装
我该有的我该做的
构造函数与析构函数
构造/析构函数 | 使用方式 |
---|---|
默认构造函数 | People a; |
People(const People &a) | 拷贝构造,与=不等价 |
People(string name); | People a(“bug”) |
~People() | 无 |
class People {
public:
People(){}
People(string name) {//有参构造
this->name = name;
}
/*People() { 默认构造函数
arr = new int[10];
cout << "default constructor" << endl;
}*/
~People() {
cout << "destructor" << endl;
}
string name;
//int *arr;
};
int main() {
People b("fangsong");
People a = string("string");
//a.arr[9] = 12
cout << a.name << endl;
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
class People {
public :
People() {}
//People() = default; // //隐藏规则变为显示规则,易于代码维护
//People() = delete;
People(string name) {
cout << "param constructor" << endl;
this->name = name;
}
People(int x) {
cout << "int param constructor" << endl;
this->x = x;
}
People(const People &a) {
cout << "copy constructor" << endl;
this->name = a.name;
this->x = a.x;
}
void operator=(const People &a) {
cout << "operator =" << endl;
this->name = a.name;
this->x = a.x;
return ;
}
~People() {
cout << "destructor" << endl;
}
string name;
int x;
};
void incr(int &a) {
a += 1;
return ;
}
int add(People a, People b) {
return a.x + b.x;
}
int main() {
int n = 7;
incr(n);
cout << n << endl;
cout << add(4, 5) << endl;
People a("hug");
People b = string("hug");
People c(543);
People d = 678;
cout << a.name << endl;
cout << b.name << endl;
cout << c.x << endl;
cout << d.x << endl;
c = 987;
cout << c.x << endl;
d = string("duanchenyang");
cout << d.name << endl;
People e = a;
cout << e.name << endl;
cout << e.x << endl;
c = a;
cout << c.name << endl;
cout << c.x << endl;
return 0;
}
类属性方法
static 类成员,类方法
类的成员与方法
class People {
public:
void say(string word);
void run(location &loc)
///
static void is_valid_height(double height);
/
private:
static int totle;
string __name;
Day __birthday;
double __height;
double __wright;
};
类属性与类方法中,类属性可以记录类对象的个数,类方法中不能访问this指针;
const方法
不去修改对象中的值
const类型对象只能调用const类型方法
修饰后只能类const访问
class People {
public:
void say(string word);
void run(location &loc)
///
string &name() const()
///
static void is_valid_height(double height);
private:
static int totle;
string __name;
Day __birthday;
double __height;
double __wright;
};
类属性与const代码演示
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <stack>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <vector>
using namespace std;
class Point {
public :
Point() {
cout << "constructor : " << this << endl;
Point::total_cnt += 1;
}
Point(const Point &a) : Point() {
cout << "copy constructor : " << this << endl;
this->x = a.x;
this->y = a.y;
}
Point(double z) : Point() {
cout << "convert constructor : " << this << endl;
this->x = 99, this->y = 99;
}
Point(int x, int y) : Point() {
cout << "param constructor : " << this << endl;
this->x = x;
this->y = y;
}
void operator=(const Point &a) {
cout << "operator= : " << this << endl;
this->x = a.x, this->y = a.y;
return ;
}
void set(int x, int y) {
this->x = x;
this->y = y;
}
void seek() const {
seek_cnt += 1;
cout << x << " " << y << endl;
}
void s() const{
cout << seek_cnt << endl;
}
static int T() { return Point::total_cnt; }//类承成员方法
~Point() {
cout << "destructor : " << this << endl;
Point::total_cnt -= 1;
}
private:
int x, y;
mutable int seek_cnt = 0;
static int total_cnt;//类方法成员属性
};
int Point::total_cnt = 0;
void test() {
Point a;
cout << Point::T() << endl;
return ;
}
int main() {
Point a;
cout << a.T() << endl;
test();
Point b;
cout << b.T() << endl;
Point c(3, 4);
cout << c.T() << endl;
Point d(3.4);
cout << d.T() << endl;
c = 5.6;
cout << c.T() << endl;
cout << &a << endl;
cout << &b << endl;
cout << &c << endl;
cout << &d << endl;
d.seek();
c.seek();
const Point e(6, 7);
e.seek();
e.seek();
e.seek();
e.seek();
e.s();
return 0;
}
深拷贝与浅拷贝
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <queue>
using namespace std;
class A {
public :
A() {
this->arr = new int[100];
}
A(const A &obj) : x(obj.x), y(obj.y) {
this->arr = new int[100];
memcpy(this->arr, obj.arr, sizeof(int) * 100);
}
int *arr;
int x, y;
};
int main() {
A a;
a.x = 3, a.y = 6;
a.arr[0] = 123;
A b = a;
b.arr[0] = 456;
cout << a.arr[0] << " " << b.arr[0] << endl;
cout << b.x << " " << b.y << endl;
b.x = 6;
cout << a.x << " " << a.y << endl;
cout << b.x << " " << b.y << endl;
return 0;
}
类型转换
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
class BigInt{
public :
BigInt() {}
BigInt(int x) {
num.push_back(x);
process_digit();
}
friend ostream &operator<<(ostream &, const BigInt &);
private:
vector<int>num;
void process_digit() {
for(int i = 0; i < num.size(); i++) {
if(num[i] < 10) continue;
if(i + 1 == num.size())num.push_back(0);
num[i + 1] = num[i]/10;
num[i] %= 10;
}
return;
}
};
ostream &operator<<(ostream &out, const BigInt &a) {
for(int i = a.num.size() - 1; i >= 0; i--) {
out << a.num[i];
}
return out;
}
void func(BigInt a) {
cout << "func : " << a << endl;
}
int main() {
BigInt a;
a = 1234;
cout << a << endl;
func(5670);
return 0;
}
运算符重载
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
namespace haizei {
class istream{
public :
istream &operator>>(int &n){
std::cin >> n;
return *this;
}
private:
};
class ostream {
public :
ostream &operator<<(int &n) {
std::cout << n;
return *this;
}
ostream &operator<<(const char *msg) {
std::cout<< msg;
return *this;
}
private:
};
istream cin;
ostream cout;
};
haizei::ostream &operator<<(haizei::ostream &out, double &z) {
std::cout << z;
return out;
}
ostream &operator+(ostream &out,const int &z){
out << z;
return out;
}
int main() {
int n, m;
haizei::cin >> n >> m;
haizei::cout << n <<" "<< m << "\n";
double k = 5.6;
haizei::cout << k << "\n";
cout + 8 + 9 + 10;
cout + k;
(((((cout + 8 )<< " ") + 9) << " ") + 10) << endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
class Point{
public:
Point() : __x(0), __y(0){}
Point(int x, int y) : __x(x), __y(y){}
int x() const {return __x;}
int y() const {return __y;}
Point operator+(const Point &a) {
/*this<---a
* a<----b
*/
return Point(x() + a.x(), y() + a.y());
}
Point &operator+=(const Point &a) {
__x += a.x();
__y += a.y();
return *this;
}
Point &operator++(){
__x +=1;
__y +=1;
return *this;
}
Point operator++(int) {
Point temp(*this);
__x += 1;
__y += 1;
return temp;
}
private:
int __x, __y;
};
ostream &operator<<(ostream &out, const Point &a) {
cout << "Point (" << a.x() << "," <<a.y() << ")";
return out;
};
int main() {
Point a(4, 5), b(3, 4), c(1, 1);
cout << a << endl;
cout << b << endl;
cout << c << endl;
cout << a << " " << b << endl;
cout << a + b << endl;
cout << ++(c += b) << endl;
cout << c << endl;
cout << c++ << endl;
cout << c << endl;
int n = 6, m = 7;
(n += m) ++;
cout << n << endl;
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
using namespace std;
class A{
public:
A() {
arr = new int[10];
}
A(const A &a) : A() {
for(int i = 0; i < 10; i++) {
this->arr[i] = a.arr[i];
}
this->x = a.x;
this->y = a.y;
return;
}
int x, y;
int *arr;
};
class B{
public :
B() : obj(nullptr) {
arr = new int[10];
arr[3] = 9973;
}
B(A *obj) : B() {
this->obj = obj;
}
int operator() (int a, int b) {
return a + b;
}
int &operator[](int ind) {
return arr[ind];
}
void operator[](const char *msg) {
cout << msg << endl;
return;
}
A *operator->() {
return obj;
}
A &operator*() {
return *obj;
}
~B() {
delete arr;
}
private:
int *arr;
A *obj;
};
ostream &operator <<(ostream &out, const A &a) {
out << "A(" << a.x << " ," << a.y << ")" << endl;
return out;
}
int main() {
B add;
cout << add(3, 4) << endl;
cout << add[3] << endl;
add[3] = 8876;
cout << add[3] << endl;
add["hello world"];
A a, b(a);
a.x = 67, a.y = 99;
B p = &a;
cout << p->x << " " << p->y << endl;
cout << *p << endl;
a.arr[3] = 9973;
b.arr[3] = 6687;
cout << a.arr[3] << endl;
cout << b.arr[3] << endl;
return 0;
}