Test Page 2 on Game Development (C++)

Keywords: C++ Game Development

Test Page 2 on Game Development (C++)

1. The function of the following prim function is to decompose the prime factor.What should be in parentheses?

void prim(int m, int n)
 {
     if (m >= n)
     {
         while (      ) n++;
         (      );
         prim(m, n);
         cout << n << endl;
     }
 }

A: m%n, m/=n.

What is the value of 2.X and why?

enum XXX{    
    x1,    
    x2,    
    x3=10,    
    x4,    
    x5,    
} x;

A: The global variable x is initialized to 0. (Local variable initialized to random value)

3. p1+5=?p2+5=?

unsigned char *p1;
unsigned long *p2;
p1=(unsigned char *)0x801000;
p2=(unsigned long *)0x810000;

A:
p1 + 5 = 0x801005;
P2+5 = 0x810028; (64-bit operating system, long 8 bytes.)Or p5 + 5 = 0x810014 (32-bit operating system, long takes 4 bytes.)

4. In a 32-bit machine, what is the output of the following code?

void example(char acWelcome[]){
    printf("%d",sizeof(acWelcome));
    return;
}
void main(){
    char acWelcome[]="Welcome to Huawei Test";
    example(acWelcome);
    return;
}

A:
4
char acWelcome[] is equivalent to char * const acWelcome, note: acWelcome is a pointer constant, not an array header address, and is an unsigned integer, which takes up only four bytes!

5. A description of virtual functions and function overloads.

A:
_Virtual functions are dynamic polymorphisms. Classes with virtual functions generate virtual function tables, class objects generate virtual function table pointers, which occupy memory space of class objects and effectively avoid ambiguity in inheritance.
_Function overload Function overload is a static polymorphism, in the process of inheritance, the subclass will inherit functions of the same name as the parent class, when class objects call the function ambiguous.
Function overload allows nonmember functions, while virtual functions do not allow functions other than nonmember functions.
The invocation of_function overload is determined by the number of parameters, the sequence (order), and the virtual function is determined by the class object.

What is the output of the following program in a 6.32-bit system?

void Func(char str_arg[100])
{
       printf("%d\n",sizeof(str_arg));
}
int main(void)
{
    char str[]="Hello";
    printf("%d\n",sizeof(str));
    printf("%d\n",strlen(str));
    char*p=str;
    printf("%d\n",sizeof(p));
    Func(str);
}

A:
6
5
4
4
Because char str[]= "Hello";Equivalent to char str[6] = {'H','e','l','l','o','\0'};When sizeof(str) is evaluated, the value is 6.And char str_arg[100] is char * const str_arg, find sizeof (str_When arg), the value is 4.

7. Assume that there are already definitions of class A,B,C,D. What is the calling order of A,B,C,D destructors in the program?

C c;
void main()
{
    A*pa=new A();
    B b;
    static D d;
    delete pa;
}

A: A B D C

The TIPS:main function is not really a program entry.On top of the underlying main function, there is also a main function interface for managing global variables, main functions, and so on.

8. Write out the output of the following program.

class A
{
public:
 void FuncA()
 {
     printf( "FuncA called\n" );
 }
 virtual void FuncB()
 {
     printf( "FuncB called\n" );
 }
};
class B : public A
{
public:
 void FuncA()
 {
     A::FuncA();
     printf( "FuncAB called\n" );
 }
 virtual void FuncB()
 {
     printf( "FuncBB called\n" );
 }
};
void main( void )
{
 B  b;
 A  *pa;
 pa = &b;
 A *pa2 = new A;
 pa->FuncA();
 pa->FuncB();
 pa2->FuncA();
 pa2->FuncB();
 delete pa2;
}

A:
FuncA called
FuncBB called
FuncA called
FuncB called

TIPS: Notice if there is a virtual function.

9. Write out the results of the following programs.

#include "stdio.h"
int sum(int a)
{
    auto int c = 0;
    static int b = 3;
    c += 1;
    b += 2;
    return (a + b + c);
}
int main()
{
    int i;
    int a = 2;
    for (i = 0; i < 5; i++) 
    { 
        printf("%d,", sum(a)); 
    } 
}

A:
8,10,12,14,16,

TIPS:auto int c = 0;Auto in can be omitted, i.e. int c = 0;

10. The output of the following program when it runs.

#include<iostream>
using namespace std;
class MyClass
{
public:
    MyClass(int i = 0)
    {
        cout << i;
    }
    MyClass(const MyClass &x)
    {
        cout << 2;
    }
    MyClass &operator=(const MyClass &x)
    {
        cout << 3;
        return *this;
    }
    ~MyClass()
    {
        cout << 4;
    }
};
int main()
{
    MyClass obj1(1), obj2(2);
    MyClass obj3 = obj1;
    return 0;
}

A:
122444
MyClass obj3 = obj1;Is a declaration statement for the initialization of obj3.This calls the copy constructor.
If obj3 already exists, for example, obj3 = obj, the assignment operator is called to overload the function and output 3.

11. What is the output of the following code?

#include<stdio.h>
char *myString()
{
    char buffer[6] = {0};
    char *s = "Hello World!";
    for (int i = 0; i < sizeof(buffer) - 1; i++)
    {
        buffer[i] = *(s + i);
    }
    return buffer;
}
int main(int argc, char **argv)
{
    printf("%s\n", myString());
    return 0;
}

A:
Random code.
Because the return value is a local variable, the buffer's scope is only within myString.

12. What are derivation and inheritance?

A:
Inheritance is primarily code reuse for parent classes.
Derivation is an abstract representation of something specific, while inheritance is the materialization of something abstract.
Derivation is for base classes and inheritance is for subclasses.

13. What is polymorphism?

A:
Unified interface implements a variety of functions.There are two main types: static and dynamic.Static polymorphism includes function overload, operator overload, and so on.Dynamic polymorphisms include virtual functions, etc.

14. Benefits of polymorphism?

A:
1 Code coupling.
(2) Provide a convenient interface for future development.
Hide underlying implementation details.
(4) Ambiguity can be avoided by dynamic polymorphism.

15. There is a vehicle base class with methods such as Start, Stop, Run, B, C, D, etc.There is a base class, AddCar, Start, Stop, Run, People A, B, C, etc.Note: Only people can drive a car, and people can drive any car (write C++ source code).

#include <iostream>

//Car base class
class ICar
{
public:
	virtual ~ICar() {}
	virtual void Start() { std::cout << "The car started!" << std::endl; }
	virtual void Run() { std::cout << "The car is running!" << std::endl; }
	virtual void Stop() { std::cout << "The car stopped!" << std::endl; }
};

//Car entity class example
class PK :public ICar
{
	void Start() { std::cout << "Pickup"; ICar::Start(); }
	void Run() { std::cout << "Pickup"; ICar::Run(); }
	void Stop() { std::cout << "Pickup"; ICar::Stop(); }
};

//Human Base Classes
class IPlayer
{
private:
	ICar *_pICar;
public:
	IPlayer(ICar *pICar)
	{
		_pICar = pICar;
	}

	void SetCar(ICar *pICar)
	{
		_pICar = pICar;
	}

	~IPlayer() {}

	virtual void Start() { _pICar->Start(); }
	virtual void Run() { _pICar->Run(); }
	virtual void Stop() { _pICar->Stop(); }
};

//Human Entity Class Example
class ZS :public IPlayer
{
public:
	//Initialize member list, call initialization parent constructor
	//That is, delegate constructor
	ZS(ICar *pICar) :IPlayer(pICar) {}

	void Start() { std::cout << "Zhang San started "; IPlayer::Start(); };
	void Run() { std::cout << "Zhang San is running "; IPlayer::Run(); };
	void Stop() { std::cout << "Zhang San stopped "; IPlayer::Stop(); };
};


int main()
{
	//Similar to the riding system in games
	PK pk;
	ZS zs(&pk);
	zs.Start();
	zs.Run();
	zs.Stop();
	return 0;
}

Posted by micki on Mon, 06 Sep 2021 09:47:28 -0700