Unit 1oops
Unit 1oops
Part -A
Introduction
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into
objects, which contain both data (attributes) and behavior (methods). It enhances code
reusability, maintainability, and scalability.
Benefits of OOP
• Code Reusability – Inheritance allows the reuse of existing code, reducing redundancy.
• Scalability – OOP makes it easier to expand and modify programs without affecting
existing code.
• Maintainability – Modular code makes debugging and updating easier.
• Security – Encapsulation ensures data hiding, preventing unintended modifications.
• Efficiency – Code can be reused, reducing development time and improving performance.
Object-Oriented Languages
Popular OOP languages include C++, Java, Python, C#, and Ruby.
Applications of OOP
OOP is widely used in:
• Software development (e.g., web and desktop applications)
• Game development
• Artificial intelligence
• Database management systems
1
KLE BCA COLLEGE GANGAVATHI C++ using Oops
// Global variable
int globalVar = 10;
// Function prototype
void displayGlobal();
// Class definition
class Sample {
//code
};
int main() {
// Local variable
int localVar = 5;
return 0;
}
// Function definition
void displayGlobal() {
cout << "Global Variable: " << globalVar << endl;
}
2
KLE BCA COLLEGE GANGAVATHI C++ using Oops
PART-B
1. Basics of C++
Components:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
// This is a single-line comment
/* This is a multi-line
comment */
cout << "Comments Example" << endl;
return 0;
}
3
KLE BCA COLLEGE GANGAVATHI C++ using Oops
● • `int`, `float`, `double`, `char`, `if`, `else`, `switch`, `for`, `while`, `return`, `class`, `public`,
`private`
Example:
Example:
#include <iostream>
using namespace std;
int main() {
const int max_value = 100;
cout << "Max Value: " << max_value << endl;
return 0;
}
2.1.1 if Statement
Executes a block of code if a condition is true.
Example:
#include <iostream>
using namespace std;
int main() {
4
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
using namespace std;
int main() {
int num = 3;
if (num % 2 == 0) {
cout << "Even number";
} else {
cout << "Odd number";
}
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int choice = 2;
switch (choice) {
case 1: cout << "Choice is 1"; break;
case 2: cout << "Choice is 2"; break;
default: cout << "Invalid choice";
}
return 0;
}
5
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Iteration: " << i << endl;
}
return 0;
}
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Iteration: " << i << endl;
i++;
}
return 0;
}
6
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "Iteration: " << i << endl;
i++;
} while (i <= 5);
return 0;
}
3. Manipulators in C++
Manipulators are used to format output in C++.
Example:
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159265;
cout << setw(10) << "Prec 3:" << setprecision(3) << pi << endl;
cout << setw(10) << "Prec 5:" << setprecision(5) << pi << endl;
return 0;
7
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
int main() {
cout << "Global x: " << ::x << endl; // 10 (global, using ::)
return 0;
Example:
#include <iostream>
using namespace std;
int main() {
int* ptr = new int(10);
cout << "Value: " << *ptr << endl;
delete ptr;
return 0;
}
8
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
using namespace std;
int main() {
int a = 5;
double b = a; // Implicit conversion from int to double
cout << "Value of b: " << b << endl;
return 0;
}
7. Operator Precedence
Determines the order of evaluation of operators.
Example:
#include <iostream>
using namespace std;
int main() {
int result = 5 + 3 * 2;
cout << "Result: " << result << endl;
return 0;
}
9
KLE BCA COLLEGE GANGAVATHI C++ using Oops
PART -C
8. Functions in C++
Function prototypes declare the function before its definition to ensure proper
compilation.
Example:
#include <iostream>
int main() {
return 0;
return x+y;
10
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
int temp = x;
x = y;
y = temp;
int main() {
int a = 5, b = 10;
swap(a, b);
return 0;
11
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
return (a > b) ? a : b;
int main() {
return 0;
Example:
#include <iostream>
return x * x;
int main() {
return 0;
12
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Example:
#include <iostream>
cout << "Hello, " << name << "!" << endl;
int main() {
return 0;
Example:
#include <iostream>
int main() {
display(num);
return 0;
13
KLE BCA COLLEGE GANGAVATHI C++ using Oops
Function overloading allows multiple functions with the same name but different
parameters.
Example:
#include <iostream>
void show(int x) {
void show(double x) {
int main() {
show(10);
show(10.5);
return 0;
14