KEMBAR78
CPP Cheatsheet | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
22 views8 pages

CPP Cheatsheet

This C++ cheatsheet covers fundamental concepts such as the differences between C and C++, inheritance, static members, destructors, and abstract classes. It also explains advanced topics like copy constructors, shallow vs deep copy, virtual functions, and memory management. Key definitions and examples are provided for each concept to aid understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views8 pages

CPP Cheatsheet

This C++ cheatsheet covers fundamental concepts such as the differences between C and C++, inheritance, static members, destructors, and abstract classes. It also explains advanced topics like copy constructors, shallow vs deep copy, virtual functions, and memory management. Key definitions and examples are provided for each concept to aid understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Cpp cheatsheet

Basic
1. What is the difference between C and C++?
C is a procedure-oriented language; C++ is object-oriented,
supports data hiding, function overloading, namespaces, and
has different memory management functions.
2. Explain inheritance.
Inheritance creates new classes (derived) from existing classes
(base), allowing code reusability by inheriting capabilities of
the base class.
3. What are the static members and static member functions?
Static members have a lifetime of the program; static functions
can be called without class instances using the class name and
scope resolution operator.
4. What are destructors in C++?
Destructors are functions automatically called when an object
is destroyed, having the same name as the class prefixed with
a tilde (~).
5. What is an abstract class and when do you use it?
An abstract class, with pure virtual functions, cannot be
instantiated and serves as a parent class for derived classes.
6. What do you mean by call by value and call by reference?
Call by value passes a copy of the parameter; call by reference
passes the address, allowing modification of the actual
argument.
7. Is destructor overloading possible? If yes then explain and if no
then why?
Destructor overloading is not possible because destructors
take no arguments, and there is only one way to destroy an
object.
8. What do you mean by abstraction in C++?
Abstraction shows essential details to the user and hides
irrelevant details.
9. What is a reference in C++?
A reference is an alias for an existing variable, allowing access
by either the variable name or the reference name.
10. Define inline function.
An inline function has its code inserted at each call point at
compile time, eliminating function calling overhead.
11. What do you know about friend class and friend function?
Friend classes and functions can access private, protected, and
public members of other classes but are not member
functions.
12. What are the different data types present in C++?
Primitive (char, short, int, float, etc.), Derived (array, pointer),
Enumeration (enum), User-defined (structure, class).
13. What are class and object in C++?
A class is a user-defined data type with data members and
member functions; an object is an instance of a class.
14. What is the difference between struct and class?
Struct members are public by default; class members are
private by default.
15. What is operator overloading?
Operator overloading modifies the default meaning of
operators for user-defined data types, enabling operations like
addition on complex numbers.
16. What is polymorphism in C++?
Polymorphism allows methods to have different behaviors in
different contexts, achieved through inheritance; types:
Compile Time and Runtime.
17. Explain constructor in C++.
A constructor is a special member function automatically
called when an object is created, with the same name as the
class.
18. Tell me about virtual function.
A virtual function is a member function in the base class
overridden in the derived class, determined at runtime using
the virtual keyword.
19. Compare compile time polymorphism and Runtime
polymorphism.
Compile-time (resolved by compiler, fast, achieved by
function/operator overloading); Runtime (resolved at runtime,
slow, achieved by virtual functions and pointers).
20. What are the C++ access specifiers?
Public: Accessible outside the class; Protected: Accessible
inside and derived classes; Private: Not accessible outside the
class.

Advanced
1. What is a copy constructor?
A copy constructor initializes an object using another object of
the same class.
class A {
public:
int x, y;
A(int x, int y) {
this->x = x;
this->y = y;
}
// Default copy constructor
};

int main() {
A a1(2, 3);
A a2 = a1; // Default copy constructor is called
return 0;
}

We can define our copy constructor; if not defined, the default


copy constructor is called.
2. What is the difference between shallow copy and deep copy?
Shallow copy stores references to the original memory address
and is faster but reflects changes in the original object.
Deep copy creates a new copy of the entire object with a
unique memory address and does not reflect changes in the
original object.
3. What is the difference between virtual functions and pure virtual
functions?
Virtual Function: A member function in the base class
redefined in a derived class, declared using the virtual
keyword.

class Base {
public:
virtual void fun() {
// Implementation
}
};

Pure Virtual Function: A function with no implementation,


declared by assigning 0, indicating it must be overridden in
derived classes.

class Base {
public:
virtual void fun() = 0; // Pure virtual function
};

4. If class D is derived from a base class B, in what order are


constructors called when creating an object of type D?
Constructors are called in the order of inheritance: first the
base class (B) constructor, then the derived class (D)
constructor.

class B {
public:
B() { cout << "Constructor of B" << endl; }
~B() { cout << "Destructor of B" << endl; }
};

class D : public B {
public:
D() { cout << "Constructor of D" << endl; }
~D() { cout << "Destructor of D" << endl; }
};

int main() {
D d; // Output: Constructor of B, Constructor of D
return 0; // Output: Destructor of D, Destructor
of B
}

5. Can we call a virtual function from a constructor?


Yes, but it always calls the version of the function in the current
class, not the derived class.

class Base {
public:
Base() {
fun(); // Calls Base::fun(), not
Derived::fun()
}
virtual void fun() {
cout << "Base fun" << endl;
}
};

class Derived : public Base {


public:
void fun() override {
cout << "Derived fun" << endl;
}
};

int main() {
Derived d; // Output: Base fun
return 0;
}

6. What are void pointers?


A void pointer is a pointer with no associated data type,
capable of holding addresses of any type.

void *ptr;
char *str;
ptr = str; // No error
str = (char*) ptr; // Typecasting required

7. What is this pointer in C++?


this is a pointer to the object itself, used to access data
members and member functions of the object.

class A {
private:
int value;
public:
void setValue(int x) {
this->value = x;
}
};

int main() {
A a;
a.setValue(5);
return 0;
}

8. How do you allocate and deallocate memory in C++?


Use the new operator for memory allocation and the delete
operator for memory deallocation.
int *value = new int; // Allocates memory for 1
integer
delete value; // Deallocates memory taken by value

int *arr = new int[10]; // Allocates memory for 10


integers
delete[] arr; // Deallocates memory occupied by arr

You might also like