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

} */

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容