類
什么是類
- 類是定義同一類所有對(duì)象的變量和方法的藍(lán)圖或原型
#include<stdio.h>
#include<iostream>
using namespace std;
class Person
{
private://訪問(wèn)修飾符
int id;
int money;
public:
void show()
{
cout<<"id = "<<id<<" money = "<<money<<endl;
}
void set(int a,int b)
{
if(a<0)
{
a=0;
}
id=a;
money=b;
}
};
int main()
{
class Person a;
class Person b;
a.set(1,1000);
a.show();
b.set(2,100);
b.show();
}
- C++的類與結(jié)構(gòu)的區(qū)別
- 在面向?qū)ο笾?算法與結(jié)構(gòu)體被捆綁在一起
定義成員函數(shù)
- 在類中定義成員函數(shù)
- 類中定義的成員函數(shù)一般為內(nèi)聯(lián)函數(shù)蹋笼,即使沒(méi)有明確用inline標(biāo)示
- 在C++中,類定義通常在頭文件中鱼鸠,因此這些成員函數(shù)定義也伴隨著進(jìn)入頭文件
- 在類之后定義(一個(gè)類占一個(gè).h配一個(gè).c)
- C++允許在其它地方定義成員函數(shù)盗忱。
- 將類定義和其成員函數(shù)定義分開惫周,是目前開發(fā)程序的通常做法颖榜。
- 我們把類定義(頭文件)看成是類的外部接口叶摄,類的成員函數(shù)定義看成是類的內(nèi)部實(shí)現(xiàn)狈究。
#include<stdio.h>
#include<iostream>
using namespace std;
class Person
{
private:
int id;
int money;
public:
void show();
void set(int a,int b);
};
void Person::show()
{
cout<<"id = "<<id<<" money = "<<money<<endl;
}
void Person::set(int a,int b)
{
if(a<0)
{
a=0;
}
id=a;
money=b;
}
int main()
{
class Person a;
class Person b;
a.set(1,1000);
a.show();
b.set(2,100);
b.show();
}
//結(jié)果為: id = 1 money = 1000
//id = 2 money = 100;
#include<iostream>
using namespace std;
class Person
{
private:
int id;
int money;
public:
void show();
void show(int x);
};
void Person::show()
{
cout<<"Person show void"<<endl;
}
void Person::show(int x)
{
cout<<"Person show "<<x<<endl;
}
void show()
{
cout<<"show void"<<endl;
}
class Test
{
public:
void show()
{
cout<<"Test show void "<<endl;
}
};
int main()
{
class Person a;
a.show();
a.show(100);
class Test b;
b.show();
}
//結(jié)果為:Person show void
//Person show 100
//Test show void
#include<iostream>
using namespace std;
class Test
{
public:
void show()
{
cout<<"Test show void "<<endl;
}
};
void lianxi1(Test *p1)
{
p1->show();
}
void lianxi2(Test &p2)
{
p2.show();
}
void lianxi3(Test p3)
{
p3.show();
}
int main()
{
Test t;
lianxi1(&t);
lianxi2(t);
lianxi3(t);
}
//結(jié)果為 Test show void
//Test show void
//Test show void
#include<iostream>
using namespace std;
class Test
{
int id;
public:
void setid(int i)
{
id=i;
}
void show()
{
cout<<"this"<<(this)<<endl;
cout<<"id = "<<this->id<<endl;
}
};
int main()
{
Test t;
t.setid(222);
t.show();
cout<<"&t = "<<&t<<endl;
}
//結(jié)果為:this 0x50
//&t = 0x50
//id = 222
封裝(數(shù)據(jù)是私有的,函數(shù)是公有的)
- 類的封裝的概念首先是碗淌,數(shù)據(jù)與算法(操作)結(jié)合,構(gòu)成一個(gè)不可分割的整體(對(duì)象)抖锥。
- 其次是亿眠,在這個(gè)整體中—些成員是被保護(hù)的,它們被有效地屏蔽磅废,以防外界的干擾和誤操作纳像。
- 另一些成員是公共的,它們作為接口提供給外界使用拯勉。而對(duì)該對(duì)象的描述即是創(chuàng)建用戶定義的類型竟趾,對(duì)C++來(lái)說(shuō)憔购,就是類的實(shí)現(xiàn)機(jī)制.
類的作用域
- 一個(gè)類的所有成員位于這個(gè)類的作用域內(nèi)
類作用域是類定義和相應(yīng)類成員定義范圍
- ::前面要加類名
封裝的優(yōu)點(diǎn)
- 重用
- 不必關(guān)心具體的實(shí)現(xiàn)
- 具有安全性
構(gòu)造函數(shù)
- C++的構(gòu)造函數(shù)和析構(gòu),使得類對(duì)象能夠輕靈地被創(chuàng)建和撤銷
- 構(gòu)造函數(shù)創(chuàng)建類對(duì)象岔帽,初始化其成員
- 析構(gòu)函數(shù)撤銷類對(duì)象
- 構(gòu)造函數(shù)和析構(gòu)函數(shù)是類的特殊成員函數(shù)
- 類的對(duì)象的初試化只能由類的成員函數(shù)來(lái)進(jìn)行
- 建立對(duì)象的同時(shí)玫鸟,自動(dòng)調(diào)用構(gòu)造函數(shù)
- 類對(duì)象的定義涉及到一個(gè)類名和一個(gè)對(duì)象名
由于類的唯一性和對(duì)象的多樣性,用類名而不是對(duì)象名來(lái)作為構(gòu)造函數(shù)名是比較合適的
練習(xí)題
- 建立一個(gè)時(shí)鐘,顯示時(shí)間
#include<iostream>
#include<stdlib.h>
#include<unistd.h>
using namespace std;
class Clock
{
int hour;
int minute;
int second;
public:
void setTime(int h,int m,int s)
{
hour=h;
minute=m;
second=s;
}
void show()
{
cout<<hour<<":";
cout<<minute<<":";
cout<<second<<endl;
}
void tick()
{
sleep(1);
if(second==59&&minute!=59)
{
minute=minute+1;
second=1;
}
else if(minute==59&&second==59&&hour!=23)
{
hour=hour+1;
minute=0;
second=1;
}
else if(hour==23&&minute==59&&second==59)
{
hour=0;
minute=0;
second=1;
}
else
{
second=second+1;
}
}
void run()
{
while(1)
{
system("clear");
show();
tick();
}
}
};
int main()
{
Clock clock;
clock.setTime(23,59,50);
clock.run();
}
- 建立一個(gè)分?jǐn)?shù)
#include<iostream>
using namespace std;
class Fraction
{
int m;
int n;
public:
void setNum(int i,int j)
{
m=i;
n=j;
}
void reduce()
{
int i;
int min=m<n?m:n;
for(i=min;i>0;i--)
{
if(m%i==0&&n%i==0)
{
break;
}
}
m=m/i;
n=n/i;
}
void show()
{
reduce();
if(m%n==0)
{
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
cout<<"m/n = "<<m<<endl;
}
else
{
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
cout<<" m/n = "<<m<<"/"<<n<<endl;
}
}
};
int main()
{
Fraction a;
int c,b;
cin>>c>>b;
a.setNum(c,b);
a.show();
}
構(gòu)造函數(shù)(析構(gòu)函數(shù)是成員函數(shù))
- 使得類對(duì)象能夠輕易地被創(chuàng)建和撤銷
#include<iostream>
using namespace std;
class Fract
{
int m_m;
int m_z;
public:
Fract()
{
m_m=16;
m_z=48;
}
void set(int m,int z)
{
m_m=m;
m_z=z;
}
void print()
{
reduce();
cout<<m_z<<"/"<<m_m<<endl;
}
void reduce()
{
for(int i=m_m;i>0;i--)
{
if((m_m%i==0)&&(m_z%i==0))
{
m_m/=i;
m_z/=i;
break;
}
}
}
};
int main()
{
Fract a;
a.print();
}
//沒(méi)有了a.set,結(jié)果為3/1;
#include<iostream>
using namespace std;
class Fract
{
int m_m;
int m_z;
public:
Fract();
void set(int m,int z)
{
m_m=m;
m_z=z;
}
void print()
{
reduce();
cout<<m_z<<"/"<<m_m<<endl;
}
void reduce()
{
for(int i=m_m;i>0;i--)
{
if((m_m%i==0)&&(m_z%i==0))
{
m_m/=i;
m_z/=i;
break;
}
}
}
};
Fract::Fract()
{
m_m=16;
m_z=48;
}
int main()
{
Fract a;
// a.set(12,16);
a.print();
}
#include<iostream>
using namespace std;
class Fract
{
int m_m;
int m_z;
public:
Fract(int m,int z)
{
m_m=m;
m_z=z;
}
void set(int m,int z)
{
m_m=m;
m_z=z;
}
void print()
{
reduce();
cout<<m_z<<"/"<<m_m<<endl;
}
void reduce()
{
for(int i=m_m;i>0;i--)
{
if((m_m%i==0)&&(m_z%i==0))
{
m_m/=i;
m_z/=i;
break;
}
}
}
};
int main()
{
Fract f1(10,20);
f1.print();
Fract f2(30,5);
f2.print();
}
#include<iostream>
using namespace std;
class Fract
{
int m_m;
int m_z;
public:
Fract(int m,int z)
{
m_m=m;
m_z=z;
}
Fract()
{
m_m=10;
m_z=100;
}
void set(int m,int z)
{
m_m=m;
m_z=z;
}
void print()
{
reduce();
cout<<m_z<<"/"<<m_m<<endl;
}
void reduce()
{
for(int i=m_m;i>0;i--)
{
if((m_m%i==0)&&(m_z%i==0))
{
m_m/=i;
m_z/=i;
break;
}
}
}
};
int main()
{
Fract f1(10,20);
f1.print();
Fract f2;
f2.print();
}
#include<iostream>
using namespace std;
class Fract
{
int m_m;
int m_z;
public:
Fract(int m,int z)
{
m_m=m;
m_z=z;
}
Fract()
{
m_m=10;
m_z=100;
cout<<"Fract construct"<<endl;
}
void set(int m,int z)
{
m_m=m;
m_z=z;
}
void print()
{
reduce();
cout<<m_z<<"/"<<m_m<<endl;
}
void reduce()
{
for(int i=m_m;i>0;i--)
{
if((m_m%i==0)&&(m_z%i==0))
{
m_m/=i;
m_z/=i;
break;
}
}
}
};
int main()
{
Fract f2[5];
}
//結(jié)果為:Fract construct
//Fract construct
//Fract construct
//Fract construct
//Fract construct
定義對(duì)象
對(duì)象的初始化
- 建立對(duì)象犀勒,須有一個(gè)有意義的初始值
C++建立和初始化對(duì)象的過(guò)程專門由該類的構(gòu)造函數(shù)來(lái)完成屎飘。
- 構(gòu)造函數(shù)給對(duì)象分配空間和初始化。
- 如果一個(gè)類沒(méi)有專門定義構(gòu)造函數(shù)贾费,那么C++就僅僅創(chuàng)建對(duì)象而不做任何初始化
- 每一個(gè)類都要保證要有一個(gè)無(wú)參的構(gòu)造函數(shù)
#include<iostream>
using namespace std;
class A
{
public:
A()
{
cout<<"A construct"<<endl;
}
};
class Test
{
A m_t;
public:
Test()
{
cout<<"Test construct"<<endl;
}
};
int main()
{
Test t;
}
//結(jié)果為:Aconstruct
//Test construct
- 一個(gè)類中的成員函數(shù)包含另一個(gè)類中的構(gòu)造函數(shù),則再創(chuàng)建類的空間時(shí),都會(huì)創(chuàng)建
#include<iostream>
using namespace std;
class A
{
int m_a;
public:
A(int a)
{
m_a=a;
cout<<"A construct "<<m_a<<endl;
}
};
class Test
{
A m_t;
public:
Test():m_t(222)
{
cout<<"Test construct"<<endl;
}
};
int main()
{
Test t;
}
//結(jié)果為:A construct 222
//Test construct
#include<iostream>
using namespace std;
class A
{
int m_a;
public:
A(int a)
{
m_a=a;
cout<<"A construct "<<m_a<<endl;
}
A()
{
}
};
class Test
{
A m_t1;
public:
Test():m_t1()
{
cout<<"Test construct"<<endl;
}
};
int main()
{
Test t;
}
//結(jié)果為:Test construct
- 當(dāng)含有多個(gè)成員變量時(shí)
#include<iostream>
using namespace std;
class A
{
int m_a;
public:
A(int a)
{
m_a=a;
cout<<"A construct "<<m_a<<endl;
}
};
class Test
{
A m_t2;
A m_t1;
public:
Test():m_t1(111),m_t2(222)
{
cout<<"Test construct"<<endl;
}
};
int main()
{
Test t;
}
//結(jié)果為:A construct222
//A construct111
//Test construct
析構(gòu)函數(shù)(有且只有一個(gè))
#include<iostream>
using namespace std;
class A
{
int m_a;
public:
A(int a)
{
m_a=a;
cout<<"A construct "<<a<<endl;
}
};
class Test
{
A m_t1;
A m_t2;
public:
Test():m_t2(222),m_t1(111)
{
cout<<"Test construct"<<endl;
}
~Test()
{
cout<<"~~~~~~Test"<<endl;
}
};
int main()
{
Test t;
cout<<"=========="<<endl;
}
//結(jié)果為:A construct 111
//A construct 222
//Test construct
//=================
//~~~~~~Test(在t消亡后才運(yùn)行)
#include<iostream>
using namespace std;
class A
{
int m_a;
public:
A(int a)
{
m_a=a;
cout<<"A construct "<<a<<endl;
}
~A()
{
cout<<"~~~~~~A "<<m_a<<endl;
}
};
class Test
{
A m_t1;
A m_t2;
public:
Test():m_t2(222),m_t1(111)
{
cout<<"Test construct"<<endl;
}
~Test()
{
cout<<"~~~~~~Test"<<endl;
}
};
int main()
{
Test t;
cout<<"=========="<<endl;
}
//結(jié)果為:A construct 111
//A construct 222
//Test construct
//=================
//~~~~~~Test
//~~~~~~A 222
//~~~~~~A 111
#include<iostream>
#include<malloc.h>
using namespace std;
class Test
{
int *p;
public:
Test()
{
p=(int *)malloc(sizeof(int));
*p=4;
cout<<"Test construct "<<endl;
}
~Test()
{
free(p);
cout<<"~~~~~~~Test"<<endl;
}
void print()
{
cout<<*p<<endl;
}
};
int main()
{
Test t;
t.print();
cout<<"========="<<endl;
}
//結(jié)果為:Test construct
//4
//======================
//~~~~~~~~Test
- 構(gòu)造函數(shù)在分配空間之后調(diào)用
bool
#include<iostream>
using namespace std;
int main(int argc,char **argv)
{
bool bSex=true;
cout<<"請(qǐng)輸入性別:";
cin>>bSex;
cout<<"我叫Jack,是一位"<<(bSex?"帥鍋":"美女")<<endl;
cout<<"true = "<<true<<endl;
cout<<"false = "<<false<<endl;
}
#include<iostream>
using namespace std;
int main()
{
string s;//string s("weiwei");//string s1(5,'a');//string s(s1);
s="hello shangqian!";//1.賦值
s+="sq1409";//2.追加/連接
// s="hello";
// s.clear();//清空
cout<<"size = "<<s.size()<<endl;//求長(zhǎng)度
cout<<"s是否為空"<<(s.empty()?"空":"非空")<<endl;
cout<<"s = "<<s<<endl;
//string類型和char *類型的轉(zhuǎn)換
const char *buf=s.c_str();
cout<<"buf = "<<buf<<endl;
//字符串比較钦购,可以直接用比較運(yùn)算符//== != > <
if(s=="hello")
{
cout<<"s == hello"<<endl;
}
//查看某個(gè)位置上的字符//at()越界訪問(wèn) 會(huì)拋出異常//[]不會(huì)
cout<<"s[3] = "<<s[3]<<endl;
// cout<<"s[4] = "<<s.at(4)<<endl;//可以通過(guò)異常捕捉
return 0;
}