Documente online.
Zona de administrare documente. Fisierele tale
Am uitat parola x Creaza cont nou
 HomeExploreaza
upload
Upload




C++ access control

visual c en


[ C++ ]

C++ access control



// BASE CLASS

class A

~A() ;

int GetP 141f513b rotectedField()

int GetP 141f513b rivateField()

protected:

int m_nP 141f513b rotectedField;

private:

int m_nP 141f513b rivateField;

// P 141f513b UBLIC

class B : public A

public:

B()

~B()

void Work()

// P 141f513b ROTECTED

class C : protected A

public:

C()

~C()

void Work()

void main()


  1. P 141f513b olymorphism

// BASE CLASS

class A

/*virtual*/ ~A()

;

/*virtual*/ void Work()

// DERIVED CLASS

class B : public A

~B()

;

void Work()

void main()

Which message will be displayed when you call pA->Work()?

a) when A::Work() is declared virtual

b) when A::Work() is declared non-virtual

c) when A::~A is declared virtual

d) when A::~A is declared non-virtual

  1. Multiple Inheritance

// BASE CLASS

class A

~A()

;

void Work()

// BASE CLASS

class B

~B()

;

void Work()

// DERIVED CLASS

class C : public A

~C()

;

void Work()

// MULTIP 141f513b LE INHERITANCE

class D : public C, public B

~B()

;

void Work()

void main()

Which messages will be displayed on the console when you make an instance of D class and call D's Work function?

  1. C++ Templates

Create a class which implements an array of int. This class should contain only one function which get an item specified by an index.

Use the same class to manage an array of floats.

Use the same class to manage an array of chars.

#include <iostream>

using namespace std;

template <class T>

class Array

T m_t[size];

public:

Array() ;

T& operator[](int index)

return m_t[index];

}

~Array() ;

void main()

for (i = 0; i < 10; i++)

  1. STL

Using STL and the Array class defined above, define an array of strings and write its items to a file.

Note: don't use standard strings (char*) and files (FILE*)

#include <iostream>

#include <strstream>

#include <string>

#include <fstream>

using namespace std;

template <class T>

class Array

T m_t[size];

public:

Array() ;

T& operator[](int index)

return m_t[index];

}

~Array() ;

void main()

for (i = 0; i < 10; i++)

  1. Namespaces

P 141f513b ut the above class in a namespace, and try to make the difference between why to use namespaces and why to not use namespaces.

#include <iostream>

#include <strstream>

#include <string>

#include <fstream>

using namespace std;

namespace ARRAY

T m_t[size];

public:

Array() ;

T& operator[](int index)

return m_t[index];

}

~Array() ;

class Array

void main()

for (i = 0; i < 10; i++)

[ Debugging ]

  1. General Issues

What is the purpose of the ASSERT macro?

ASSERT() is supposed to evaluate its parameter and, if this is zero, to break the execution. In release mode, ASSERT does nothing.

What is the purpose of the VERIFY macro?

VERIFY() runs exactly like ASSERT, but it runs in both debug and release modes.

What is the purpose of the TRACE macro?

TRACE() is the counterpart of printf(), except that it prints to the debug window. It also does nothing in release mode.

  1. Visual Studio Debugging

_ASSERT and _ASSERTE

The CRTDBG.H file defines those two macros for assertion checking. What is the difference between them?

_ASSERT: if the expression is evaluated to false (or zero), its result is the filename and the line of the assertion.

_ASSERTE is more powerful because it returns the filename and the line of the assertion, as well as the expression that turned out to be false. This may be enough to identify the problem without referring to the source code. However, the Debug version of your application will contain a string constant for each expression asserted using _ASSERTE. If you use many _ASSERTE macros, these string expressions take up a significant amount of memory. If that proves to be a problem, use _ASSERT to save memory.

#define _ASSERTE(expr) \
do while (0)


Define Your Own Assertion

Knowing that it should run only in debug version (and it should not increase the code in release version), define your own ASSERT macro.

#ifdef _DEBUG
void _Assert(char *, unsigned int); //! prototype of the function

#define ASSERT (f) \
if (f) \
\
else \
_Assert(__FILE__, __LINE__)
#else
#define ASSERT (f)
#endif

void _Assert(char *strFile, unsigned int nLine)


Why should we use assertion?

Use assertion to validate function arguments. Let's take as example, the memcpy function.

void *memcpy(void *pvTo, void *pvFrom, size_t size)


Don't assume that the function will always be called with non-NULL parameters or that blocks are never overlapping.


Complete the following function to eliminate random behavior and to for bugs to be reproducible.
bool NewMemory(void **ppv, size_t size)


Sometimes it runs successfully, but sometimes it doesn't. And you don't know how to handle. If you leave the contents undefined, it makes bugs hard to reproduce. So, you could modify the code like he following:

#define byteGarbage    0xA3
bool NewMemory(void **ppv, size_t size)

#endif

return (*ppb != NULL);


For reference, "Writing Solid Code" - Steve Maguire (Cap.: Fortify Your Subsystems)

P 141f513b revent bugs to appear. There are many cases when it's not healthy to write a code and to waste time for debugging. Write a function to transform a character into its lower correspondent.

// Statistically
// . more than 75% developers would write the following function
char tolower(char ch)


// . others would write:
char tolower(char ch)




int chNew;
if ((chNew = tolower(ch)) != -1)
ch = chNew;

// . and others, if they require the argument to be uppercase, would write:
char tolower(char ch)


What's happening when a function is called with 0?
First function:     tolower(0) = 0 + 97 - 65 = 32;
Second function:    tolower(0) = -1;
Third function:    tolower(0) . ASSERTION FAILED.




Describe what the following function means (its behavior).

void SendP 141f513b resentForChildren(const char *strChildName, const unsigned int &nChildAge)


Actually, there is a major bug inside. Anything else of the content was added to capture your attention. Did you find the bug?


Describe the behavior of the following function.

double Divide(int n1, int n2)


Actually the above code contains a critical bug: division by zero.


Document Info


Accesari: 1341
Apreciat: hand-up

Comenteaza documentul:

Nu esti inregistrat
Trebuie sa fii utilizator inregistrat pentru a putea comenta


Creaza cont nou

A fost util?

Daca documentul a fost util si crezi ca merita
sa adaugi un link catre el la tine in site


in pagina web a site-ului tau.




eCoduri.com - coduri postale, contabile, CAEN sau bancare

Politica de confidentialitate | Termenii si conditii de utilizare




Copyright © Contact (SCRIGROUP Int. 2024 )