c^M^M
c^M^M
instances of classes. These objects contain data (attributes) and methods (functions) that operate on the data. OOP
helps in organizing code, promoting reusability, scalability, and maintainability.
cpp
CopyEdit
#include <iostream>
class Animal {
public:
void sound() {
};
public:
void sound() {
};
int main() {
Dog d;
Animal is a class
Class support ✅ Has classes and objects ❌ No classes (uses objects only)
javascript
CopyEdit
var person = {
name: "Alice",
age: 25,
greet: function() {
};
🔚 Summary:
(i) Methods
🔸 (i) Methods
✳️Definition:
A method is a function that is defined inside a class and operates on the objects of that class. Methods are used to
define behaviors or actions that an object can perform. They help achieve encapsulation and improve code
modularity.
✳️Types of Methods:
✳️Key Features:
✅ Example (C++):
cpp
CopyEdit
#include <iostream>
class Student {
private:
string name;
int age;
public:
name = n;
age = a;
void displayData() {
cout << "Name: " << name << ", Age: " << age << endl;
};
int main() {
Student s1;
return 0;
✳️Output:
yaml
CopyEdit
🔸 Real-life Analogy:
Think of a remote control. The buttons are methods — each one performs a specific task (like increasing volume or
changing channels). The TV is the object being controlled.
✳️Definition:
An abstract class is a class that cannot be instantiated directly. It is meant to be inherited by other classes. It serves
as a template or base for other classes.
An abstract class must contain at least one pure virtual function – a function declared with = 0.
✳️Features:
Promotes abstraction.
Used when you want to enforce a contract (rules) that derived classes must follow.
✅ Example (C++):
cpp
CopyEdit
#include <iostream>
// Abstract class
class Shape {
public:
};
// Derived class
public:
void draw() {
};
// Another Derived class
public:
void draw() {
};
int main() {
Shape* s1;
Circle c;
Square s;
s1 = &c;
s1 = &s;
return 0;
✳️Output:
css
CopyEdit
Drawing a Circle
Drawing a Square
🔸 Real-life Analogy:
Imagine Shape is a general concept. You can’t "draw" a generic shape — it must be specified (like a Circle or Square).
Similarly, an abstract class cannot be used directly; it needs a specific implementation in derived classes.
🟩 Conclusion:
Contains Logic to manipulate object data One or more pure virtual functions
Feature Methods Abstract Classes
🔹 What is a Function? How to declare and define functions in C++? Explain with examples.
🔸 Definition of a Function:
A function in C++ is a block of code that performs a specific task. Functions are used to divide a large program into
smaller, manageable, and reusable pieces.
It helps in:
Code reusability
Modularity
Readability
🔸 Function Components:
1. Declaration (Prototype)
2. Definition
3. Call
✅ 1. Function Declaration (Prototype):
Tells the compiler about the function name, return type, and parameters before it is used.
cpp
CopyEdit
return_type function_name(parameter_list);
🔹 Example:
cpp
CopyEdit
✅ 2. Function Definition:
cpp
CopyEdit
return a + b;
✅ 3. Function Call:
cpp
CopyEdit
🔸 Complete Example:
cpp
CopyEdit
#include <iostream>
// Declaration
// Definition
int add(int a, int b) {
return a + b;
int main() {
int x = 5, y = 10;
return 0;
🔹 Output:
makefile
CopyEdit
Sum: 15
No arguments, with return value Takes nothing, returns value int getNumber()
Arguments with return value Takes values, returns value int add(int a, int b)
cpp
CopyEdit
void greet() {
int main() {
greet();
return 0;
}
🔸 Advantages of Using Functions:
Avoids repetition
🟩 Conclusion:
A function is a reusable, named block of code that performs a specific task. In C++, functions improve the
organization, readability, and efficiency of code. Knowing how to declare, define, and call functions is a fundamental
skill in C++ programming.
🔹 3. List and explain the different variables and operations used in C++ with examples. Explain briefly the
importance of Scope Resolution Operator.
🔸 I. Variables in C++
A variable in C++ is a named memory location used to store data that can be modified during program execution.
Local Variable Declared inside a function/block, accessible only there int x = 10; inside main()
Type Description Example
Global Variable Declared outside all functions, accessible globally int count = 0; at top of program
Static Variable Retains its value between function calls static int n = 0;
Register Variable Stored in CPU register for faster access register int speed = 10;
Constant Variable Value cannot be changed once assigned (const) const float pi = 3.14;
✅ Example:
cpp
CopyEdit
#include <iostream>
void show() {
count++;
int main() {
cout << "Global: " << globalVar << ", Local: " << localVar << endl;
show(); // Count: 1
show(); // Count: 2
return 0;
C++ supports many types of operators to perform operations on variables and data.
✅ 1. Arithmetic Operators:
+ Addition a+b
- Subtraction a-b
* Multiplication a * b
/ Division a/b
% Modulus a%b
✅ 2. Relational Operators:
== Equal to a == b
!= Not equal to a != b
✅ 3. Logical Operators:
` `
✅ 4. Assignment Operators:
✅ 5. Increment/Decrement Operators:
✅ 6. Bitwise Operators:
Operator Meaning
& AND
` `
^ XOR
~ NOT
✅ 7. Ternary Operator:
cpp
CopyEdit
Example:
cpp
CopyEdit
✅ Definition:
Access global variables when local variables with the same name exist.
cpp
CopyEdit
#include <iostream>
int main() {
return 0;
Output:
sql
CopyEdit
Local x: 20
Global x: 50
cpp
CopyEdit
class MyClass {
public:
void display();
};
void MyClass::display() {
🟩 Conclusion:
C++ provides various types of variables and operators to manipulate data efficiently. Functions like the Scope
Resolution Operator are crucial for maintaining clear scope control and improving code structure, especially in
object-oriented and modular programming.
An inline function is a function where the compiler replaces the function call with the actual code of the function at
compile-time (if possible).
It is suggested to the compiler using the inline keyword.
🔸 Syntax:
cpp
CopyEdit
// function body
✅ Example:
cpp
CopyEdit
#include <iostream>
return x * x;
}
int main() {
return 0;
🔸 Output:
scss
CopyEdit
Square of 4: 16
When a function is called, normally the control jumps to that function, executes the code, and returns back
— this causes overhead.
With inline functions, this jump is avoided, as the code is inserted directly where the function is called (like a
macro).
This can increase execution speed for small functions, but may increase code size (if overused).
Function Call Code is inserted at call location Control jumps to function code
Speed Faster execution for small functions Slower due to function call overhead
Memory Usage May increase memory (code duplication) Less memory as function exists once
Debugging Harder to debug due to no actual call Easier to trace and debug
cpp
CopyEdit
int square(int x) {
return x * x;
}
int main() {
✅ Same as Inline:
cpp
CopyEdit
return x * x;
But here, the call is replaced by 5 * 5 during compilation (if compiler agrees).
🟩 Conclusion:
An inline function helps to reduce function call overhead and is useful for small, simple functions. However, it
should be used wisely to avoid increased code size.
The main difference from a normal function is that inline functions avoid jumping to a separate block of code by
replacing the function call with its actual body during compile time.
✅ Definition:
Tokens are the smallest individual units in a C++ program. When we write a C++ program, the compiler breaks it
down into meaningful elements called tokens.
Keywords Reserved words with special meaning in C++ int, return, if, class
Constants/Literals Fixed values that do not change 10, 3.14, 'A', "Hello"
✅ Example:
cpp
CopyEdit
In the above program, each word/symbol is a token used by the compiler to understand the structure of the code.
✅ Definition:
✅ Syntax:
cpp
CopyEdit
✅ Example:
cpp
CopyEdit
#include <iostream>
int main() {
int x = 10;
cout << "x after modifying ref: " << x << endl;
return 0;
🔹 Output:
yaml
CopyEdit
x: 10
ref: 10
cpp
CopyEdit
int temp = a;
a = b;
b = temp;
int main() {
int x = 5, y = 10;
swap(x, y);
cout << "x: " << x << ", y: " << y << endl; // Output: x: 10, y: 5
a and b are reference variables — changes made inside the function affect the original variables.
🟩 Conclusion:
Tokens are the building blocks of a C++ program. Without them, the compiler can't understand the code.
Reference variables offer an efficient way to manipulate data without copying. They improve performance
and help write cleaner, more efficient programs.