发新话题
打印

我考的auto程序题附答案

面霸应届生求职网欢迎您!

我考的auto程序题附答案

程序都在机子上跑过了
1.
#include "stdafx.h"
#include <iostream>
using namespace std;

bool F(int *p1, int p2)
{
bool status=false;
p2*=2;
p1=&p2;

status=true;
return status;
}

void main()
{
int a=0;
int b=12;
bool result=F(&a,b);
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"result="<<result<<endl;


}

答案:
a=0
b=12
result=1

2.
#include "stdafx.h"
#include <iostream>
using namespace std;

class A
{
public:
virtual ~A() {f();}
virtual void f(){cout<<"A::f() is called"<<endl;}
protected:
private:
};

class B:public A
{
public:
~B(){};
virtual void f(){cout<<"B::f() is called"<<endl;}
};

void main()
{
B *b=new B;
delete b;
}

答案:
A::f() is called


3.
// Auto.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class Person
{
public:
static int person_num;
Person() {person_num++;}
~Person()
{
person_num--;
print();
}
void print() {cout<<"the person_num is "<<person_num<<endl; }
protected:
private:
};

int Person::person_num=0;
Person func(Person x)
{
x.print();
return x;
}

int main(int argc, char* argv[])
{
Person p1;
p1.print();
Person p2=func(p1);
p2.print();

//printf("Hello World!
");
return 0;
}

答案:
the person_num is 1
the person_num is 1
the person_num is 0
the person_num is 0
the person_num is -1
the person_num is -2
4.
#include "stdafx.h"
#include <iostream>
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),c(n){}
~Derived(){cout<<c;}
};

int main()
{
Derived(X
return 0;
}

答案:
XY
发新话题