首页/ 题库 / [单选题]有如下程序:#include<iostr的答案

有如下程序:#include<iostream>using namespace std;class Point{public: static int number;public: Point(){number++;} ~Point(){number--;}};int Point::number=0;void main(){ Point*ptr; Point A,B; { Point*ptr_point=new Point[3]; ptr=pb_point; } Point C cout<<Point::number<<endl; delete[]ptr; }运行时输出的结果是

单选题
2022-06-13 17:45
A、3
B、4
C、6
D、7
查看答案

正确答案
C

试题解析
解析:本题考查的知识点是:静态成员,对象的构造,对象的生存周期。静态成员的特性是不管这个类创建了多少个对象,它的静态成员都只有一个拷贝(副本),这个副本被所有属于这个类的对象共享。本题的Point类中就定义了一个静态成员变量 number。其初始值为0,每次构造则自动增1,析构则自动减1,所以number是Point类对象的计数值。在主函数中,第1行定义了1个Point类型指针ptr,这并不会构造 Point类对象,因此number值为0;第2行定义了2个Point对象,所以此时number值为2;第3~6行是一个语句块,其中通过new运算符又定义了1个包含3个Point对象元素的数组,由于是动态创建的,所以其生命周期只能通过delete运算符来结束,否则会一直占据内存直到程序结束,所以现在number的值为5:第7行定义了1个 Point对象C,number的值变为6;第8行输出number的内容,所以最后输出的结果是6。故本题应该选择C。

标签:
感兴趣题目
有如下程序: #include<iostream> using namespace std; class VAC{ public: int f() const{return 3;} int f() {return 5;} }; int main(){ VAC v1; const VAC v2; cout<<v1.f()<<v2.f(); return 0; } 运行时的输出结果是( )。
有如下程序: #include<iostream> using namespace std; class VAC{ public; intf()const{return 3;} int f(){return 5;} }; Int main(){ VAC v1; const VAC v2; cout<<v1.f()<<v2.f(); return 0; } 运行时的输出结果是( )。
有下列程序: #include<iostream> using namespace std; c1ass TestClass { private; char c; public: TestClass(char n):c(n){} ~TeStClass() { cout<<c } }; class TestClass1:public TestClass {
有如下程序: #include <iostream> using namespace std; class XX { protected: int k; public: XX(int n=5):k(n){} ~XX() { cout<<"XX"; } virtual void f() const=0; }; inline void XX::f()
有如下的程序: #include<cstring.h) #ingclude<iostream.h) using namespace std; Class MyString {public: MyString(const char*s); ~MyString(){delete[]data;} protected: unsigned len; char*data;}; MyString::MyString(const char*S) {l
有下列的程序: #include<cstring.h> #include<iostream.h> using namespace std; class MyString { public: MyString(const char*s); ~MyString()<delete[]data;} protected: unsigned len; char*data; };
有以下程序: #include <iostream> #include <string> using namespace std; int main () { char s[]=" 123\"; cout<<strlen (s) <<", "<<sizeof (s) <<end1; return 0; }
有如下程序: #include <iostream> using namespace std; int main() { int *p; *p = 9; cout << "The value at p: " << *p; return 0; } 编译运行程序将出现的情况是( )。
有如下程序: #include <iostream> using namespace std; int main( ){ int*P; *P=9; cout<<"The value at P:"<<*P: return 0; } 编译运行程序将出现的情况是
有如下程序: #include<iostream> using namespace std; int main() { int *p; *p=9; cout<<"The value at p:"<<*p; return 0; } 编译运行程序将出现的情况是( )
有以下程序: #include <iostream> #include <string> using namespace std; class base { private: char baseName[10]; public: base ( ) { strcpy (baseName, "Base"); } virtual char *myName() {
有如下程序: #include<iostream> using namespace std; classA { public: A(){cout<<"A";} }; class B{public:B(){cout<<"B";}}; class C:public A { B b; public: C(){cout<<"C";} }; int main(){C obj;return 0;} 执行后的输出结果是( )
相关题目
若有如下程序: #include<iostream> using namespace std; class TestClass { public: void who(){cout<<“TestClass”<<endl;} }; class TestClassl:public TestClass { public: void who(){cout<<“TestClassl”<<endl; int main() { TestC
下面程序的运行结果为( )。 #include <iostream> using namespace std; class A{ public: A(){cout<<" ";} ~A(){cout<<" ";} } class B:public A{ public: B(){cout<<" ";} ~B(){cout<<" ";} } void main(){ B b; }
有如下程序: #include <iostream> using namespace std; class AA { public: virtual void f() { cout<< "AA"; } }; class BB : public AA { public: BB() { cout << "BB"; } }; cla
有如下程序: #include <iostream> using namespace std; class XX { protected: int k; public: XX(int n=5):k(n){} ~XX() { cout<<"XX"; } virtual void f() const=0; }; inline void XX::f()
有下列程序: #include<iostream> Using namespace std; Class Amount{ int amount; public; Amount(int n=O):amount(n){} Int getAmount()const{return amount;} Amount &operator+=(AmountA) {
有如下程序: #include<iostream> using namespace std; class MyClass{ public: MyClass(){++count;} ~MyClass(){--count;} static int getCount(){return count;} private: static int count; }; int MyClass::count=0; int main(){ MyCl
若有以下程序: #include <iostream> using namespace std; class point { private: int x, y; public: point ( ) { x=0; y=0; } void setpoint(int x1,int y1) { x=x1; y=y1;
有如下程序: #include <iostream> using namespace std; Class x { protected: int a; public: x() { a=1; } }; class x1 : virtual public x { public: x1() { a+=1; cout<<
有如下程序: #include<iostream) using namespace std; classBase{ public: Base(int x=0){cout<<x;} }; Class Derived:public Base{ public: Derived(int x=0){cout<<x;} private: Base Val; }; int main(){ Derived d(1); return
有下列程序: #include<iostream> using namespace std; class TestClass { int a; public: TestClass(int x)<a=x;} void show(){cout<<a;} }; class TestClass1:publicTestClass { int b; public: TestCla
有如下程序: #include<iostream> using namespace std; class Base{ public: Base(int x=0){cout<<x;} }; class Derived:public Base{ public: Derived(int x=0){cout<<x;} private: Base val; }; int main( ){ Derived d(1); return 0; } 程序的输出结果是
有如下程序: #include <iostream> using namespace std; class Base { public: Base(int x=0) { cout<<x; } } class Derived: public Base{ public: Derived(int x=0) { cout<<x; } private: Base val; }; int main() { Derived d(1); return 0; }程序的输出结果是
有如下程序: #include<iostream> using namespace std; Class TestClass {int a; public: TestClass(int x){a=x;} void show(){cout<<a;}}; class TestClass1:public TestClass {int b; public: TestClass1(int i):TestClass(i+1),b(i){} voi
有如下程序: #include <iostream> using namespace std; class AA { public: virtual void f() { cout<< "AA"; } }; class BB : public AA { public: BB() { cout << "BB"; } }; cla
如下程序的输出结果是 #include<iostream> using namespace std; class A{ public: A( ){cout<<"A";} }; class B{public:B( ){cout<<"B";}}; class C:public A{ B b; public: C( ){cout<<"C";} }; int main( ){C obj;return 0;}
有如下程序: #include<iostream> using namespace std; class A { public: A(){cout<<"A";} }; class B<public:B(){cout<<"B";)); class C:public A { B b; public: C(){cout<<"C";} }; int main(){Cobj;retum 0;} 执行后的输出结果是
有如下程序: #include<iostream>#include<iosream> using namespace std; class BASE{ char c; public; BASE(char n):c(n){} virtual ~ BASE(){cout<<c;} }; class DERIVED; public BASE{ char c; public: DERIVED (char n): BASE (n+1)
有以下程序: #include <iostream> using namespace std; class BASE { private: char c; public: BASE(char n):c(n);{} virtual~BASE() { cout<<c; } }; class DERIVED:public BASE { char c; p
有如下程序: #include<iostream> using namespace std; Class Test { public: Test() {n+=2;} ~Test() {n-=3;} static int getNum() {return n;} private: static int n; }; int Test::n=1; int main() { Test*p=new Test;
下面程序的运行结果为( )。 #include <iostream> using namespace std; class A{ public: A(){cout<<" ";} ~A(){cout<<" ";} } class B:public A{ public: B(){cout<<" ";} ~B(){cout<<" ";} } void main(){ B b; }
广告位招租WX:84302438

免费的网站请分享给朋友吧