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

//一填空题

/*

1.将关键字const写在函数参数列表之后,函数体之前。说明该函数

是常成员函数。

2一个const对象只能访问常量成员函数。

3.在函数题之前加const关键字可以放置覆盖函数改变数据成员的值。

二。单向选择题。

1.简单成员函数的声明一般不含关键字static

2.已知print()函数是一个类的长成员函数。无返回值(voidprint() const)

3.下列函数中不是类成员函数的是(友元函数)

4.对友元函数描述正确的是(A一个类的友元函数是在该类中说明的一个函数)

5.改错题

staticint get()

{

return number;

}

test * two={new test(4,5),test(6,8)}

*/

/*指向类成员函数的指针

#include

using namespace std;

class A{

private:

int val;

public:

A(int i){

val=i;

}

intvalue(int a){

returnval+a;

}

};

int main()

{

int (A::*pfun)(int);

}

*/

//第五章特殊函数和成员

/*5.11仍然使用类Test但改用对象指针数组的演示主程序

class Test{

int num;

double f1;

public:

Test(int n)

{

num=n;

}

Test (int n,double f)

{

num=n;

f1=f;

}

int getNum()

{

return num;

}

double getF()

{

return f1;

}

};

#include

using namespace std;

int main()

{

Test * one[2]={new Test(2),new Test(4)};

Test * two[2]={new Test(1,3.2),new Test(5,9.5)};

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

cout<getNum()<

cout<getF()<

}

}

*/

//数组和类的使用。

/*5.10使用类对象数组和指针的例子

class Test{

int num;

double f1;

public:

Test(int n)

{

num=n;

}

Test (int n,double f)

{

num=n;

f1=f;

}

int getNum()

{

return num;

}

double getF()

{

return f1;

}

};

#include

using namespace std;

int main()

{

Test one[2]={2,5},*p = nullptr;

Test two[2]={Test(1,3.2),Test(5,9.6)};

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

cout<<""<

p=two;

}

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

cout<getNum()<getF()<

}

}

*/

/*const函数返回的变量对象与其他常量对象一起使用

#include

using namespace std;

class ConstFun

{

public:

int f5() const {

return 5;

}

int Obj()

{

return 45;

}

};

int main()

{

ConstFun s;

const int i=s.f5();

const int j=s.Obj();

int x=s.Obj();

int y=s.f5();

cout<

ConstFun d;

int k=d.f5();

cout<

}

*/

/*4 const对象。

常量成员。

5.7常数据成员初始化和常饮用作为函数参数。

#include

using namespace std;

class Base

{

private:

int x;

//const int a;

static const int b;

const int & r;

public:

Base(int,int);

void show()

{

cout<

}

void display(const double & r)

{

cout<

}

};

const int Base::b=125;

Base::Base(int i,int j):x(i),r(x)

{

};

int main()

{

Base a(102,33),a2(110,90);

a.show();

a2.show();

a2.display(3.1);

}

*/

/*2

常引用作为函数参数。void Display(const double &r);

3.常对象在对象名前使用const声明常对象,但声明时必须同时进行初始化,而且不能被

更新。定义的语法。类名const对象名(参数表)

4.常成员函数

可以声明一个成员函数是const函数。一个const对象可以调用const函数。但不能调用非

const成员函数。const放在函数声明之前意味着返回值是常量。

类型标示符函数名(参数列表)const;

类型表识符类名::函数名(参数列表)const{函数体}

类型标识符函数名(参数列表)const{函数体}

*/

/*5,8

const对象调用const成员函数。

#include

using namespace std;

class Base{

private:

double x,y;

const double p;

public:

Base(double m,double n,double d):p(d)

{

x=m;y=n;

}

void show();

void show() const;

};

void Base::show()

{

cout<

}

void Base::show() const

{

cout<

}

int main()

{

Base a(8.9,2.5,1416);

const Base b(8.9,2.5,1416);

b.show();

a.show();

return 1;

}

*/

/*3.将一个类说明为另一个类的友元

#include

using namespace std;

class Two{

public:

int y;

friend class one;

};

class One{

int x;

public:

One(int a,Two &r,int b)

{

x=a;

r.y=b;

}

void Display(Two & r)

{

cout<

}

};

int main()

{

Two obj;

One obj1(23,obj,55);

obj1.Display(obj);

return 1;

}

*/

/*将成员函数用作友元

一个类的成员的函数包括构造函数和析购函数可以通过friend说明为另一个类的友元,,将类one的成员

函数func说明为two的友元因为func时属于类one的。所以要使用限定符说明它的出处。即one::fune(Two&)

one的对象可以通过友元函数func(two&)访问类TWO的成员。因为是访问类two应使用two对象的饮用作为传递参数。

类one对象通过友元函数访问类two的对象的私有数据。

#include

using namespace std;

class Two;

class One

{

private:

int x;

public:

One(int a)

{

x=a;

}

int getx()

{

return x;

}

void func(Two&);

};

class Two{

private:

int y;

public:

Two(int b){

y=b;

}

int gety()

{

return y;

}

friend void One::func(Two &);

};

void One::func(Two& r)

{

r.y=x;

}

int main()

{

One obj1(5);

Two obj2(8);

cout<

obj1.func(obj2);

cout<

return 1;

}

*/

/*友元函数

有时两个概念上相近的类,要求其中一个类可以无限制地存取另一个类的成员。例如一个链表的实现需要一个节点类描述单个节点,另一个链表类涌来处理链表本身。链表类的成员由管理者存在。

友元函数解决了这类难题。友元函数可以存取私有成员共有成员保护成员。其实友元函数可以是一个类或函数

尚未定义的类也可以作为友元饮用。

//类本身的友元函数。

#include

using namespace std;

#include

class Point

{

private:

double x,y;

public:

Point(double x1,double y1)

{

x=x1;

y=y1;

}

double getX()

{

return x;

}

double getY()

{

return y;

}

friend double distances(Point &,Point &);

};

double distances(Point & a,Point & b)

{

double dx=a.x-b.x;

double dy=a.y-b.y;

return sqrt(dx*dx+dy*dy);

}

int main()

{

Point p(2,3),p2(4,8);

cout<<"dsa"<

return 1;

}

*/

/*静态对象

#include

using namespace std;

class test{

private:

int n;

public:

test(int i)

{

n=i;

cout<

}

int getn()

{

return n;

}

void inc()

{

++n;

}

};

int main()

{

cout<<"loop"<

for (int i=0; i<3; i++) {

static test a(3);

test b(3);

a.inc();

b.inc();

cout<

cout<

}

return 1;

}*/

/*静态成员

简单成员是指声明中不含const volatilestatic关键字的函数。如果类的数据成员

或成员函数使用关键字static进行修饰。这样的成员称为静态数据成员或静态成员函数。统称为静态成员。

5.2

class Test{

static int x;

int m;

public:

Test(){};

Test(int a,int b){

x=a;

m=b;

}

static int func()

{

return x;

}

static void sfunc(Test&r,int a)

{

r.m=a;

}

int getm()

{

return m;

}

};

int Test::x=25;

#include

using namespace std;

int main()

{

cout<

Test b,c;

b.sfunc(b,58);

cout<

cout<

Test a(24,56);

cout<<""<

}

*/

/*

1.对象成员的初始化。

5.1分析下面程序中析购函数与构造函数的调用顺序。

#include

using namespace std;

class object

{

private:

int val;

public:

object():val(0)

{

cout<<"默认构造函数1"<

}

object(int i):val(i)

{

cout<<"构造函数有参数1"<

}

~object()

{

cout<<"析构函数1"<

}

};

class container{

private:

object one;

object two;

int data;

public:

container():data(0)

{

cout<<"默认构造2"<

}

container(int i,int j,int k);

~container()

{

cout<<"析构函数2"<

}

};

container::container (int i,int j,int k)

{

data=k;

cout<<"构造函数有参数2"<

}

int main()

{

container obj,anObj(5,6,10);

} */

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容