C++考試第二章 (第二天)

/*

2.1成員函數(shù)的實(shí)例。

2.2封裝的實(shí)例。

2.3構(gòu)造函數(shù)初始化結(jié)構(gòu)的對(duì)象。

2.4從結(jié)構(gòu)演變成一個(gè)簡(jiǎn)單的類。

2.8String的使用。

2.9 string的使用substr和find的使用May 28,2002

2.11演示string對(duì)象的例子string的反轉(zhuǎn)和復(fù)制的使用對(duì)象數(shù)組和泛型算法

2.12string.beignstring.end升冪復(fù)制交換swap find查找

2.13 string數(shù)組

第二章的作業(yè)題

一,1.string類建立對(duì)象的不正確的方式是

1.string str("OK") 2.string str ="OK" 3.string str; 4.string str='OK';

2.下面所列各項(xiàng)中不是面向?qū)ο蟪绦蛟O(shè)計(jì)所具特點(diǎn)的選項(xiàng)是

1.封裝2.繼承3.抽象4.函數(shù)。

二,

1.已知一個(gè)學(xué)生類具有性別和年齡,男學(xué)生張明年齡12歲女學(xué)生李紅的年齡是11歲。

classStudent{

string sex;

int age;

setSexandAge(string s,int a)

{

sex=s;age=a;

}

}

student stu1; student stu2;

stu1.setSexandAge("男",12);

stu2.setSexandAge("女",11);

2,一個(gè)圓具有圓心坐標(biāo)和半徑兩個(gè)屬性,并且能夠給出圓的面積。

classCircularity

{

Point p;

float radii;

float getActcreage()

{

reurn radii*radii*3.14;

}

}

3.畫出一個(gè)班級(jí)類圖電話卡的類圖

class PubClass{

string no;

int num;

}

class Card

{

long no;

float balance;

}

三。編程題

1.兩個(gè)字符串的鏈接。

string str1="2323";

string str2="ssss";

copy (&str[0],&str[0]+4,&str2[4]);

string str1="2323";

string str2="ssss";

string str3=str1+str2;

copy (&str1[0],&str1[0]+4,&str2[4]);

cout<

cout<

cout<

char c1[]={"11111"};

char c2[10]={"22222"};

char c3[30];

int i=0,k=0;

for (i=0; i<20; i++) {

c3[i]='\0';

}

i=0;

while (c1[i]!='\0') {

c3[k]=c1[i];

i++;

k++;

}

i=0;

while (c2[i]!='\0') {

c3[k]=c2[i];

i++;

k++;

}

cout<

2."we are here"輸出h

string str2=str1.substr(7,1);

char *p =find(str.begin(),str.end(),'h');

if(p)

{

cout<<*p;

}

*/

/*2.13 string數(shù)組*/

#include

#include

#include

usingnamespacestd;

intmain()

{

//string str[]={"dsasad","sadds"};

//for (int i=0; i<2; i++) {

//copy(str[i].begin(), str[i].end(), ostream_iterator(cout));

//cout<

//}

//str[0].swap(str[1]);

//for (int i=0; i<2; i++) {

//cout<

//}

//

}

/* 2.12string.beignstring.end升冪復(fù)制交換swap find查找

#include

#include

using namespace std;

int main()

{

string s1="adass";

string s2="sad";

//升冪

sort(s1.begin(), s1.end());

//降冪

sort(s2.begin(), s2.end(), greater());

//交換

swap(s1, s2);

//find查找

cout<<(*(find(s1.begin(), s1.end(), 'd'))=='d');

return 0;

}

*/

/*2.11演示string的反轉(zhuǎn)和復(fù)制的使用

#include

using namespace std;

int main()

{

string s="here you are->";

string s2=s;

//反轉(zhuǎn)和輸出

reverse(&s[0],&s[0]+14);

//復(fù)制輸出

cout<< s2;

copy(&s[0],&s[0]+14,&s2[0]);

cout<< s2;

//copy(&s2[0], &s2[0]+14, ostream_iterator(cout));

return 0;

}

*/

/*2.9 string

#include

using namespace std;

int main()

{

string input;

getline(cin, input,'\n');//May 28,2002

cout<

//輸入輸出

//截取

int index_x= input.find(" ");

cout<

int index_y=input.find(",");

cout<

cout<

return 0;

}

*/

/*演示string隊(duì)形初始化的例子

#include

using namespace std;

int main()

{

string str1=("dsadsa");

string str2="dsadsa2";

string str3;

cout<<"輸入一個(gè)字符串";

cin>>str3;

cout<

//字符查找find()find("",0);0位置開始

}

*/

/*2.4從結(jié)構(gòu)演變成一個(gè)簡(jiǎn)單的類

#include

using namespace std;

class Point {

private:

double a;double b;

public:

Point(){};

Point (double x,double y){

a=x;b=y;

}

void setXY(double x,double y){

a=x;b=y;

}

void display()

{

cout<

}

};

int main()

{

Point a;

a.setXY(12, 13);

Point b=Point(12, 134);

a.display();

b.display();

}*/

/*2.3構(gòu)造函數(shù)初始化結(jié)構(gòu)的對(duì)象。

#include

using namespace std;

struct Point {

private:

double x,y;

public:

Point(){};

Point(double a,double b)

{

x=a;y=b;

}

void setXY(double a,double b)

{

x=a;y=b;

}

void display()

{

cout<

}

};

int main()

{

Point p1;

Point p2=Point(134,45);

p1.setXY(123, 34);

p1.display();

p2.display();

return 0;

}*/

/* 2.2封裝的實(shí)例

#include

using namespacestd;

struct Point {

private:

double x,y;

public:

void setXY(double a,double b)

{

x=a;y=b;

}

void disPlay()

{

cout<

}

};

int main()

{

Pointp;

p.setXY(12,34);

//錯(cuò)誤的cout<

}

*/

/*2.1成員函數(shù)的實(shí)例

#include

using namespace std;

struct Point

{

double x,y;

void setXY(double a,double b)

{

x=a;

y=b;

}

void disPlay()

{

cout<

}

};

int main()

{

Point p;

p.setXY(12, 124);

p.disPlay();

return 0;

}

*/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容