当前位置:   article > 正文

C++ Primer 学习笔记 第七章 类_#include #include #include

#include #include #include using namespace std;
233 定义类
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Sales_data {
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0;

	string isbn() const 
	{ 
		return bookNo;
	};

	Sales_data& combine(const Sales_data&);
	double avg_price() const;
};

//  在类外定义类方法, const为方法内部访问对象成员的方式为只读
double Sales_data::avg_price() const {
	if (units_sold) {
		return revenue / units_sold;
	}
	else {
		return 0;
	}
}

// 修改对象成员
Sales_data& Sales_data::combine(const Sales_data& rhs) {
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}


int main() {
	Sales_data total;
	total.units_sold = 2;
	total.revenue = 8;
	cout << total.avg_price() << endl;

	Sales_data sub;
	sub.units_sold = 1;
	sub.revenue = 4;

	total = total.combine(sub);
	cout << total.units_sold << " " << total.revenue << endl;

	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
233 练习7.4
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Person {
	string name;
	string address;
	string get_name() const;
	string get_addr() const;
	Person& modify_name(const Person& person, string new_name);
};

string Person::get_name() const {
	return name;
}

string Person::get_addr()const {
	return address;
}

Person& Person::modify_name(const Person& person, string new_name) {
	name = new_name;
	return *this;
}


int main() {
	Person person;
	person.name = "ethan";
	person.address = "fangcun";
	cout << person.get_name() << " " << person.get_addr() << endl;
	person.modify_name(person, "tony");
	cout << person.name << endl;
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
234 类的非成员函数
#include<iostream>
#include<vector>
#include<string>

using namespace std;

struct Sales_data {
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0;

	string isbn() const
	{
		return bookNo;
	};

	Sales_data& combine(const Sales_data&);
	double avg_price() const;

	istream& read(istream& is, Sales_data& item);
	ostream& print(ostream& os, const Sales_data& item);
	
	Sales_data& add(const Sales_data& lhs, const Sales_data& rhs);

};

//  在类外定义类方法, const为方法内部访问对象成员的方式为只读
double Sales_data::avg_price() const {
	if (units_sold) {
		return revenue / units_sold;
	}
	else {
		return 0;
	}
}

// 修改对象成员
Sales_data& Sales_data::combine(const Sales_data& rhs) {
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

// read 函数
istream& read(istream& is, Sales_data& item) {
	double price = 0;
	is >> item.bookNo >> item.units_sold >> price;
	item.revenue = price * item.units_sold;
	return is;
}

// print函数
ostream& print(ostream& os, const Sales_data& item) {
	os << item.isbn() << " " << item.units_sold << " " << item.revenue << " "  << item.avg_price();
	return os;
}

// add函数
Sales_data& Sales_data::add(const Sales_data& lhs, const Sales_data& rhs) {
	Sales_data sum = lhs;
	sum.combine(rhs);
	return sum;
}

int main() {
	Sales_data total;
	total.units_sold = 2;	
	total.revenue = 8;
	cout << total.avg_price() << endl;

	Sales_data sub;
	sub.units_sold = 1;
	sub.revenue = 4;

	total = total.combine(sub);
	cout << total.units_sold << " " << total.revenue << endl;

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
237构造函数
#include<iostream>
#include<vector>
#include<string>
using namespace std;


struct Sales_data {
	Sales_data() = default;
	Sales_data(const string& s) :bookNo(s) {}
	Sales_data(const string &s, unsigned n, double p):
		bookNo(s), units_sold(n), revenue(p*n){}
	Sales_data(istream&);

	string isbn() const { return bookNo; }
	Sales_data& combine(const Sales_data&);
	double avg_price() const;
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0;

};


int main() {

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
239 练习7.15
#include<iostream>
#include<vector>
#include<string>
using namespace std;


class Person {
public:
	Person() = default;
	Person(const string nm, const string ad):name(nm), address(ad){}
	string getName() const { return this->name; }
	string getAddress() const { return this->address;}
	ostream& print(ostream&, const Person&);
	ostream& read(istream&, Person&);

public:
	string name;
	string address;

};

int main() {

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
240 访问说明符public 与 private
#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Sales_data {
public:
	Sales_data() = default;
	Sales_data(const string& s, unsigned n, double p):
		bookNo(s), units_sold(n), revenue(p*n){}
	Sales_data(const string &s):bookNo(s){}
	Sales_data(istream&);
	string isbin() const { return bookNo; }
	Sales_data& combine(const Sales_data&);

private:
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
	double avg_price() const {
		return units_sold ? revenue / units_sold : 0;
	}
};



int main() {

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
241 友元 friend其他函数成为此类的友元,访问非公有成员
#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Sales_data {

friend Sales_data add(const Sales_data&, const Sales_data&);
friend istream& read(istream&, Sales_data&);
friend ostream& print(ostream&, const Sales_data&);

public:
	Sales_data() = default;
	Sales_data(const string &s, unsigned n, double p):
		bookNo(s), units_sold(n), revenue(p*n){}
	Sales_data(string &s):bookNo(s){}
	Sales_data(istream&);
	string isbn() const {
		return bookNo;
	}
	Sales_data& combine(const Sales_data&);

private: 
	string bookNo;
	unsigned units_sold = 0;
	double revenue = 0.0;
};

// 类外声明非成员函数
Sales_data add(const Sales_data&, const Sales_data&);

istream& read(istream&, Sales_data&);

ostream& print(ostream&, const Sales_data&);


int main() {
	return 0;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
243 7.22 完整Person类实现
#include<iostream>
#include<string>

using namespace std;


class Person {

public:
	Person() = default;
	Person(const int a,  const int n):age(a), num(n), total(a*n){}
	int get_total() const { return total; }

private:
	int num = 4;
	int age = 10;
	int total;
};


int main() {
	Person person(3, 10);
	cout << person.get_total() << endl;

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
243 定义某种类型在类中的别名
#include<iostream>
#include<string>

using namespace std;


class Screen {
public:
	typedef string::size_type pos;
private:
	pos cursor = 0;
	pos height = 0, width = 0;
	string contents;

};

int main() {
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
245 类的其他属性
#include<iostream>
#include<string>
#include<vector>

using namespace std;


class Screen {
public:
	typedef string::size_type pos;
	Screen() = default;
	Screen(pos ht, pos wd, char c):height(ht), width(wd),contents(ht*wd,c){}
	char get() const {
		cout << "没有参数的get()" << endl;
		return contents[cursor];
	}
	inline char get(pos ht, pos wd) const;
	Screen& move(pos r, pos c);
	void some_number() const;

private:
	pos cursor = 0;
	pos height = 0, width = 0;
	string contents;
	// mutbale修饰后不会受到const的影响
	mutable rsize_t access_ctr;
	vector<Screen> screens{ Screen(24, 80, ' ') };
};

inline
Screen& Screen::move(pos r, pos c)
{
	pos row = r * width;
	cursor = row + c;
	return*this;
}
char Screen::get(pos r, pos c) const {
	pos row = r * width;
	cout << "有参数的get()" << endl;
	return contents[row + c];
}

void Screen::some_number() const {
	// 因为access_ctr使用mutable修饰,即使在const对象以内也能够被修改
	++access_ctr;
}


int main() {
	Screen myscreen;
	// 根据入参选择对应的成员函数
	char ch = myscreen.get();
	ch = myscreen.get(0, 0);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
250 练习7.3.3
#include<iostream>
#include<string>

using namespace std;

class Y;

class X {
	Y* y = nullptr;
};

class Y {
	X x;
};


int main() {

	system("pause");

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
251 类之间的友元关系
#include<iostream>
#include<string>
#include<vector>
using namespace std;


class Screen {
public:
	friend class Window_mgr;
	Screen() = default;
	Screen(string::size_type ht, string::size_type wd, char c) :height(ht), width(wd), contents(ht* wd, c){}
	

private:
	string::size_type cursor = 0;
	string::size_type height = 0, width = 0;
	string contents;

};

class Window_mgr {
public:
	typedef vector<Screen>::size_type ScreenIndex;
	void clear(ScreenIndex);
private:
	vector<Screen> screens{ Screen(24, 80, ' ') };
};

void Window_mgr::clear(ScreenIndex i) {
	Screen& s = screens[i];
	s.contents = string(s.height * s.width, ' ');
}




int main() {
	
	cout << "hi" << endl;
	return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
253 练习 7.32
#include<iostream>
#include<string>
#include<vector>
using namespace std;

class Screen;

class Window_mgr {
public:
	void clear(Screen&);
};



class Screen {
public:
	Screen() = default;
	Screen(int lgt):length(lgt){}
	friend void Window_mgr::clear(Screen&);
	int get_length();

private:
	int length;

};


inline int Screen::get_length() {
	return length;
}

void Window_mgr::clear(Screen&s) {
	s.length = 0;
};


int main() {
	Screen screen(20);
	
	cout << screen.get_length() << endl;
	Window_mgr win;
	win.clear(screen);
	cout << screen.get_length() << endl;
	return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
类变量的查找
#include<iostream>
#include<string>
#include<vector>

using namespace std;


int height=1;
class Screen {
public:
	typedef std::string::size_type pos;
	void dummy_fcn(pos height) {
		// 用函数参数的height
		// cursor = width * height;
		// 3
		// 用类里面定义的height
		// cursor = width * this -> height;
		// cursor = width * Screen::height;
		// 2
		// 用全局指定的height
		//cursor = width * ::height;
		// 1
		cout << cursor << endl;
	}
private:
	pos cursor = 0;
	pos height = 2, width = 1;
};



int main() {
	Screen screen;
	screen.dummy_fcn(3);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
260 有默认值的构造函数
#include<iostream>
#include<string>
#include<vector>

using namespace std;


class Sales_data {
public:
	// 不传参时候访问此函数
	Sales_data(string s = "") :bookNo(s) { cout << "default"; }
	// 传参时候访问此函数
	Sales_data(string s, unsigned cnt, double rev):
		bookNo(s), units_sold(cnt), revenue(rev* cnt) {
		cout << "custom";
	}
private:
	string bookNo;
	unsigned units_sold;
	double revenue;
};


int main() {

	Sales_data data("hello", 3, 2);
	
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
261 委托构造函数
#include<iostream>
#include<string>
#include<vector>

using namespace std;


// 委托构造函数
class Sales_data {
public:
	Sales_data(string s, unsigned cnt, double price) :bookNo(s), units_sold(cnt), revenue(cnt* price)
	{
		cout << "执行了委托构造函数" << endl;
	}
	// 委托一
	Sales_data() : Sales_data("", 0, 0) { cout << "执行委托1" << endl; }
	// 委托二
	Sales_data(string s):Sales_data(s, 0, 0){ cout << "执行委托2" << endl; }
	// 委托三
	Sales_data(istream &is):Sales_data() { cout << "执行委托3" << endl; }


private:
	string bookNo;
	unsigned units_sold;
	double revenue;

};



int main() {
	Sales_data data1("dfd", 2, 3);
	Sales_data data2("sfsd");
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
联系7.43
#include<iostream>
#include<string>
#include<vector>

using namespace std;


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

class C {
public:
	C():no(0){}

private:
	NoDefault no;
};


int main() {

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
263 隐式的类类型转换
#include <iostream>
#include <string>
#include <vector>
using namespace std;


class Sales_data {
public:
	Sales_data() = default;
	Sales_data(const string &s, unsigned n, double p) :bookNo(s), units_sold(n), revenue(n*p) {};
	// 没有抑制构造函数进行隐式转换
	Sales_data(string &s):bookNo(s){}

	// 抑制构造函数进行隐式的转换
	// explicit Sales_data(string &s):bookNo(s){}

	explicit Sales_data(istream&){}
	void combine(const Sales_data& new_item);

	string bookNo;
	unsigned units_sold;
	double revenue;

};

void Sales_data::combine(const Sales_data& new_item) {
	cout << "成功通过隐式转换来构建Sales_data对象" << endl;
	cout << "构造后的对象的bookNo值" << endl;
	cout << new_item.bookNo << endl;
}


int main() {
	cout << "hello" << endl;
	Sales_data item("sss", 2, 4);
	string book = "aa";
	item.combine(book);
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

266 聚合类

#include <iostream>
#include <string>
#include <vector>
using namespace std;

// 聚合类
struct Data {
	int ival;
	string s;
};


int main() {
	// 类初始化必须按照声明的顺序
	Data val = {0, "Anna"};
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
271 类的静态成员
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Account {
public:
	// 类的静态成员,必须含有const
	static const int amount = 1;
	static const int external_amount;
	void get_amount() {
		// 类里面可以随便访问
		cout << amount << endl;
	}
	static void method() {
		cout << "static method" << endl;
		return;
	}

	static void external_method();
};

// 在类的外部定义的静态方法不用再写static了
void Account::external_method() {
	cout << "static external method" << endl;
}
// 外部定义静态成员值
constexpr int Account::external_amount=5;

//class Bar() {
//private:
//	static Bar mem1;
//}



int main() {
	// 访问方式一,类访问运算符号方式
	cout << Account::amount << endl;
	// 访问方式二,对象 . 的方式访问
	Account ac1;
	cout << ac1.amount << endl;
	// 访问方式三, 类指针访问成员符号方式
	Account *ac2 = &ac1;
	cout << ac2->amount << endl;
    
	// 共享静态成员
	Account ac3;
	cout << ac3.amount << " " << ac1.amount << endl;

	// 静态方法
	ac3.method();

	return 0;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/136468
推荐阅读
相关标签
  

闽ICP备14008679号