1、C).类的其他函数可以调用类的静态成员函数D).类的静态数据成员被类的所有对象共享14以下关于类和对象叙述正确的是_。A).一个类只能有一个对象B).一个对象可以属于几个不同的类C)对象是类的一个具体实例(实体)D)一个类中可以有本类的对象18_将在对象生存期结束时由系统自动调用。A) 构造函数 B) 成员变量函数 C) 析构函数 D) 虚函数19下面关于友员函数的说法中,正确的是 _ 。A)友员函数是本类的一个成员函数 B)友员函数可以通过对象访问本类的所有成员 C)友员函数有this指针D)友员函数可以直接访问本类的所有成员20假定AB为一个类名,则执行AB a(4),b5,*p; 语句时
2、,自动调用该类构造函数的次数为 。A)5 B)6 C)7 D)9二阅读程序, 写出以下程序运行结果。1.#include lass myclass int a,b; public: myclass (int i=0,int j=0); void show( ); ; myclass: myclass(int i,int j) a=i; b=j; void myclass:show( ) a+=b; b+=a;cout”a=”a ”b=”bendl; void main() myclass ob1; ob1.show(); myclass ob2(2); ob2,show(); myclass
3、ob3(2,5); ob3.show(); 运行结果: 2#include class cube public:cube(int ht=2,int wd=5,int dp=3) height=ht; width=wd; depth=dp;depth=depthconstruncted calledcube() coutDestructed calledint volume() return height *width*depth;private: int height,width,depth;void main() cube cone(10,20,15), ctwo;cone.volume()
4、ctwo.volume()运行结果:3写出以下程序运行结果。class Apublic:A() A(int i,int j):a(i),b(j) coutconstructorn;A() coutlead1lead2;total=p1*lead1+p2*lead2;void display() couttotal=total TC A,B(6,8);A.datap();A.display();5写出以下程序运行结果。#includeclass value int a; value()a=1; cout”Defaultn”;value(int i)a=i;”constructuren”;void
5、 list( )cout” “; value( ) cout”Destructuren”;void main( ) value data14;value data24=10,20,30;for(int i=0;i4;i+) data1i.list();for(i=0;i+) data2i.list();6写出以下程序运行结果。 A(int i,int j) a=i;constructor.n;A( ) coutDestructor int a,b; A *pa,*pb;pa=new A(4,6);pb=new A(10,15);delete pa;delete pb;7写出以下程序运行结果。c
6、lass countcount () count1+;static int data () return count1;count( ) count1-; static int count1;int count :count1=60; count a,b,c,d,e;count: data()a.data()e.data()8写出以下程序运行结果。class myclass int x,y;static long sum; myclass(int a,int b) x=a;y=b;void getxy() sum+=x*y;sum=sumA(char *str);A( ); char stri
7、ng50;A:A(char *str) strcpy(string,str);constructor called forstringA()destructor called forvoid fun() A funobject(Fun对象);static A staticobject(static对象In fun()A Globalobject(G对象 /全局对象 A Mainobject(Main对象In Main(), before calling funnfun( );In Main(), after calling funn10写出以下程序运行结果。class sample int x
8、,y; sample()x=0;y=0;sample(int i,int j)x=i;y=j;void copy(sample &A) x=A.x; y=A.y;void display()x=xendl y=yclass stringclass stringclass (char *str);stringclass();void showstring(); char *ptr;long len;stringclass:stringclass(char *str) len=strlen(str);ptr=new charlen+1;if(!ptr) coutAllocution errorn;
9、 exit(1);strcpy(ptr,str);stringclass()Freeing Allocutionn;deleteptr;void stringclass:showstring()ptrlen stringclass s1(C+ programming);stringclass s2(Test string);s1showstring();s2二 程序填空1编写一个求n!的类,分别输出29的阶乘。class FACT int n;long int fact;FACT(int );void display( ) coutn!=_ _ 1) fact*= _ ; val-; for(
10、int i=2;10;i+) FACT p(i); p.display();2以下程序能根据对象提供的长方形的长和宽得到该长方形的面积和周长。class rectangle float ledge,sedge;rectangle()ledge=0;sedge=0;rectangle(float a,float b) ledge=a; sedge=b;void area()cout”面积:” _ _ void length()cout”周长: _ _ rectangle A(3.5, 2.0),B(4.2, 2.0);A.area();A.length();B.area();B.length()
11、;3填空完成以下程序,使该程序的执行结果为10。class MyClass int x; x=n; int GetNum() ;void main() MyClass my(10);my.GetNum()4以下程序能实现求a2+b2+c2。其中a、b、c分别为5、10、15由不同对象的初始化值提供。该程序使用静态成员实现。class myclassmyclass(int x);void getnumber();static void Result();int a;_ _ ; /定义静态数据成员summyclass:myclass(int x)a=x;sum+=a*a;void myclass:
12、getnumber( )Number=Result( )Result=_ _ ; /static数据成员初始化void main()myclass ob1(5),ob2(10),ob3(15);ob1.getnumber();ob2.getnumber();ob3.getnumber();_ _; /调用Result输出结果5以下程序能求出n个不同字符串的总长度。程序中定义一个类,其中数据成员length记录每一个字符串的长度,静态数据成员total-length记录所有字符串的总长度,用静态成员函数求总长度。class stringstring(char *s);static int get
13、_total_length(string a);string() void show(); int length;static int total_length;char str80;string:string(char *s) /构造函数实现 length=strlen(s); strcpy(str,s);int string:get_total_length(string a) total_length+=a.length ;return total_length ;_ _ ;void string:show()str length=length string ob1(“The first
14、 abcde”);ob1.show();Total=get_total_length(obl)string ob2(“The second fghijklmn”);ob2.show();Total length=get_total_length(ob2)string ob3(program end!);ob3.show();Total_length=get_total_length(ob3)The first abcde length=15Total=15The second fghijklmn length=20Total length=35program end! length=12Tot
15、al_length=476某单位经销的货物,购进和卖出以重量为单位,以下程序记下目前库存货物的总重量(total_weight),在横线填空并写出运行结果.class goods static int total_weight;int weight; goods(int w) weight=w; total_weight +=w;goods(goods &gd) weight=gd.weight; total_weight+=weight;goods( ) total_weight -= weight; total_weightw;goods g1(w);” 第一种货物重”g1.get()”输
16、入第二种货物重量:goods g2(w);” 第二种货物重”g2.get()”货物总重量是:”; goods:gettotal( ) ob2.value2 )return 1;else if ( _ _ return -1;else return 0; classone ob1(40);classtwo ob2(15);if(function(ob1,ob2)=1) coutob2.valuen;else if( function(ob1,ob2)= =-1 ) coutob1.valueelse coutclass point public: point(double a,double b) x=a; y=b; double setx( ) return x; double sety( ) return y; friend double dist(point &d1,poin
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1