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 ;
} ;
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 ;
}
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 ;
}
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) ;
} ;
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 ;
}
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;
}
ostream& print ( ostream& os, const Sales_data& item) {
os << item. isbn ( ) << " " << item. units_sold << " " << item. revenue << " " << item. avg_price ( ) ;
return os;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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 ;
}
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;
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;
}
int main ( ) {
Screen myscreen;
char ch = myscreen. get ( ) ;
ch = myscreen. get ( 0 , 0 ) ;
return 0 ;
}
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 ;
}
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 ;
}
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 ;
}
类变量的查找
# 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) {
cout << cursor << endl;
}
private :
pos cursor = 0 ;
pos height = 2 , width = 1 ;
} ;
int main ( ) {
Screen screen;
screen. dummy_fcn ( 3 ) ;
return 0 ;
}
260 有默认值的构造函数
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 ;
}
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 ;
}
联系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 ;
}
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 ( 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 ;
}
266 聚合类
# include <iostream>
# include <string>
# include <vector>
using namespace std;
struct Data {
int ival;
string s;
} ;
int main ( ) {
Data val = { 0 , "Anna" } ;
return 0 ;
}
271 类的静态成员
# include <iostream>
# include <string>
# include <vector>
using namespace std;
class Account {
public :
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 ( ) ;
} ;
void Account :: external_method ( ) {
cout << "static external method" << endl;
}
constexpr int Account:: external_amount= 5 ;
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 ;
}