Babu
Babu
Babu
Data types in C++ specify the type of data that a variable can hold. Here are the primary
Example:
int num = 5;
char letter = 'A';
float decimal = 5.99;
double largeDecimal = 9.87654321;
bool isTrue = true;
Variables are storage locations with a specific type and associated name. The life and scope
• Scope: The region of the code where the variable can be accessed. It can be local or global.
• Lifetime: The duration for which the variable exists in memory.
Example:
#include <iostream>
using namespace std;
void myFunction() {
int localVar = 5; // Local scope
cout << localVar << endl;
}
int main() {
myFunction();
cout << globalVar << endl;
return 0;
}
3. Read Array
An array is a collection of items stored at contiguous memory locations. To read an array in C++, you
Example:
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; i++) {
cout << arr[i] << " ";
}
return 0;
}
4. Define Pointer
Example:
#include <iostream>
using namespace std;
int main() {
int var = 20;
int *ptr = &var; // Pointer to var
cout << "Value of var: " << var << endl;
cout << "Address of var: " << ptr << endl;
cout << "Value at ptr: " << *ptr << endl; // Dereferencing
return 0;
}
• Call-by-Value: Copies the actual value of an argument into the formal parameter of the function.
• Changes made to the parameter do not affect the argument.
• Call-by-Reference: Copies the address of an argument into the formal parameter. Changes to the
• parameter affect the argument.
Example:
#include <iostream>
using namespace std;
void callByValue(int x) {
x = 100;
}
int main() {
int a = 5;
callByValue(a);
cout << "After callByValue: " << a << endl; // Output: 5
callByReference(a);
cout << "After callByReference: " << a << endl; // Output: 100
return 0;
}
Dynamic memory allocation allows you to allocate memory at runtime using the new operator and
Example:
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // Allocate memory
*ptr = 50;
cout << "Value: " << *ptr << endl;
delete ptr; // Deallocate memory
return 0;
}
7. Explain String
In C++, a string is an object that represents a sequence of characters. The std::string class in
the C++ Standard Library provides a set of functions for working with strings.
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string greeting = "Hello, World!";
cout << greeting << endl;
return 0;
}
• Static Object Creation: Objects are created on the stack. Their lifetime is limited to
• the scope in which they are created.
• Dynamic Object Creation: Objects are created on the heap using the new operator.
• They persist until explicitly deleted using the delete operator.
Example:
#include <iostream>
using namespace std;
class MyClass {
public:
void display() {
cout << "Hello, World!" << endl;
}
};
int main() {
MyClass obj; // Static object creation
obj.display();
A static variable retains its value between function calls and is initialized only once.
Example:
#include <iostream>
using namespace std;
void increment() {
static int count = 0; // Static variable
count++;
cout << "Count: " << count << endl;
}
int main() {
increment();
increment();
increment();
return 0;
}
Output:
Copy code
Count: 1
Count: 2
Count: 3
Examples:
cpp
Copy code
#include <iostream>
using namespace std;
class Base {
public:
int publicVar; // Public access specifier
protected:
int protectedVar; // Protected access specifier
private:
int privateVar; // Private access specifier
};
int main() {
Base b;
b.publicVar = 1; // Accessible
// b.protectedVar = 2; // Not Accessible
// b.privateVar = 3; // Not Accessible
return 0;
}
Constructors and Its Types
Types of Constructors:
to initialize an object.
#include <iostream>
using namespace std;
class Example {
public:
int a, b;
// Default Constructor
Example() {
a = 0;
b = 0;
cout << "Default Constructor Called" << endl;
}
// Parameterized Constructor
Example(int x, int y) {
a = x;
b = y;
cout << "Parameterized Constructor Called" << endl;
}
// Copy Constructor
Example(const Example &obj) {
a = obj.a;
b = obj.b;
cout << "Copy Constructor Called" << endl;
}
};
int main() {
Example e1; // Default Constructor
Example e2(10, 20); // Parameterized Constructor
Example e3 = e2; // Copy Constructor
return 0;
}
when an object of that class is destroyed. It has the same name as the
class, preceded by a tilde (~).
class Example {
public:
Example() {
cout << "Constructor Called" << endl;
}
~Example() {
cout << "Destructor Called" << endl;
}
};
int main() {
Example e1;
return 0;
}
Example:
#include <iostream>
using namespace std;
class Example {
public:
int a, b;
// Default Constructor
Example() {
a = 0;
b = 0;
}
// Parameterized Constructor
Example(int x, int y) {
a = x;
b = y;
}
int main() {
Example e1; // Calls Default Constructor
Example e2(10, 20); // Calls Parameterized Constructor
Example e3(30); // Calls Another Parameterized Constructor
return 0;
}
A friend class or friend function can access private and protected members
of another class. Declaring a friend breaks the encapsulation principle of
class Example {
private:
int a;
public:
Example() : a(10) {}
friend void showA(Example &e);
};
int main() {
Example e;
showA(e);
return 0;
}
Friend Class Example:
#include <iostream>
using namespace std;
class ClassB;
class ClassA {
private:
int a;
public:
ClassA() : a(10) {}
friend class ClassB;
};
class ClassB {
public:
void showA(ClassA &obj) {
cout << "Value of a: " << obj.a << endl;
}
};
int main() {
ClassA objA;
ClassB objB;
objB.showA(objA);
return 0;
}
Example:
#include <iostream>
using namespace std;
class Example {
private:
int a;
public:
Example(int a) {
this->a = a; // Using this to refer to the current object's a
}
void showA() {
cout << "Value of a: " << this->a << endl;
}
};
int main() {
Example e(10);
e.showA();
return 0;
}
Answer:
Inheritance is a fundamental concept in object-oriented programming
(OOP) where a new class, called a derived or child class, is created
based
on an existing class, known as the base or parent class. The child
class inherits the properties and behaviors (methods) of the parent
class, allowing for code reuse and the creation of hierarchical relationships.
The types of inheritance include:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
2. Provide a detailed explanation of its types with examples.
Answer:
2.1 Single Inheritance
Single inheritance occurs when a class inherits from one base class.
class Animal {
public:
void eat() {
cout << "Eating" << endl;
}
};
class Bird {
public:
void fly() {
cout << "Flying" << endl;
}
};
class Animal {
public:
void eat() {
cout << "Eating" << endl;
}
};
class Animal {
public:
void eat() {
cout << "Eating" << endl;
}
};
Run-time Polymorphism
Run-time polymorphism is achieved through inheritance and virtual functions.
It is resolved during run time.
Example:
void display(int i) {
cout << "Integer: " << i << endl;
}
void display(double d) {
cout << "Double: " << d << endl;
}
void display(string s) {
cout << "String: " << s << endl;
}
In this example, the display function is overloaded with different
parameter types.
Example:
class Complex {
public:
int real, imag;
Complex(int r, int i) : real(r), imag(i) {}
class Base {
public:
virtual void display() {
cout << "Display from Base" << endl;
}
};
class Base {
public:
virtual void display() {
cout << "Display from Base" << endl;
}
};
Pure Virtual Function
A pure virtual function is a function declared in a base class that
has no definition relative to the base class.
class Base {
public:
virtual void display() = 0; // Pure virtual function
};
Abstract Class
An abstract class is a class that cannot be instantiated and usually
contains at least one pure virtual function.
class Base {
public:
virtual void display() = 0; // Pure virtual function
};
class Base {
public:
virtual void show() { cout << "Base" << endl; }
};