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

/*

2.1成员函数的实例。

2.2封装的实例。

2.3构造函数初始化结构的对象。

2.4从结构演变成一个简单的类。

2.8String的使用。

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

2.11演示string对象的例子string的反转和复制的使用对象数组和泛型算法

2.12string.beignstring.end升幂复制交换swap find查找

2.13 string数组

第二章的作业题

一,1.string类建立对象的不正确的方式是

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

2.下面所列各项中不是面向对象程序设计所具特点的选项是

1.封装2.继承3.抽象4.函数。

二,

1.已知一个学生类具有性别和年龄,男学生张明年龄12岁女学生李红的年龄是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,一个圆具有圆心坐标和半径两个属性,并且能够给出圆的面积。

classCircularity

{

Point p;

float radii;

float getActcreage()

{

reurn radii*radii*3.14;

}

}

3.画出一个班级类图电话卡的类图

class PubClass{

string no;

int num;

}

class Card

{

long no;

float balance;

}

三。编程题

1.两个字符串的链接。

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数组*/

#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升幂复制交换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的反转和复制的使用

#include

using namespace std;

int main()

{

string s="here you are->";

string s2=s;

//反转和输出

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

//复制输出

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队形初始化的例子

#include

using namespace std;

int main()

{

string str1=("dsadsa");

string str2="dsadsa2";

string str3;

cout<<"输入一个字符串";

cin>>str3;

cout<

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

}

*/

/*2.4从结构演变成一个简单的类

#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构造函数初始化结构的对象。

#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封装的实例

#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);

//错误的cout<

}

*/

/*2.1成员函数的实例

#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;

}

*/

你可能感兴趣的:(C++考试第二章 (第二天))