0% found this document useful (0 votes)
6 views14 pages

Unit 1oops

The document provides comprehensive notes on Object-Oriented Programming (OOP) and C++ programming concepts. It covers basic OOP principles, benefits, and applications, along with detailed explanations of C++ program structure, control structures, functions, and memory management. Additionally, it includes examples for clarity on various topics such as class definitions, loops, and function overloading.

Uploaded by

shashikarna430
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)
6 views14 pages

Unit 1oops

The document provides comprehensive notes on Object-Oriented Programming (OOP) and C++ programming concepts. It covers basic OOP principles, benefits, and applications, along with detailed explanations of C++ program structure, control structures, functions, and memory management. Additionally, it includes examples for clarity on various topics such as class definitions, loops, and function overloading.

Uploaded by

shashikarna430
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/ 14

KLE BCA COLLEGE GANGAVATHI C++ using Oops

Notes on Object-Oriented Programming (OOP)


UNIT-1

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.

Basic Concepts of OOP


1. Class – A blueprint for creating objects.
2. Object – An instance of a class with specific data and behavior.
3. Encapsulation –Bundling data & methods inside a class.
4. Abstraction –Hiding complex details show the necessary details (e.g., using `private`
members).
5. Inheritance – New class inherits properties from a existing class.
6. Polymorphism – Allowing methods to take multiple forms (e.g., overloading and
overriding).

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

Structure of a C++ Program


#include <iostream>
using namespace std;

// Global variable
int globalVar = 10;

// Function prototype
void displayGlobal();

// Class definition
class Sample {

//code
};

int main() {
// Local variable
int localVar = 5;

cout << "Local Variable: " << localVar << endl;


displayGlobal();//function calling

return 0;
}

// Function definition
void displayGlobal() {
cout << "Global Variable: " << globalVar << endl;
}

A typical C++ program consists of:


1. Preprocessor directives (e.g., `#include <iostream>`)
2. The `main()` function (execution starts here)
3. Class and object definitions
4. Function implementations
5. Input/output operations

Creating, Compiling, and Linking a C++ Program


1. Write the C++ code and save it as a `.cpp` file.
2. Compile using a compiler like g++ (`g++ program.cpp -o program`).
3. Link the compiled files to create an executable.
4. Run the executable to execute the program.

2
KLE BCA COLLEGE GANGAVATHI C++ using Oops

PART-B
1. Basics of C++

1.1 The iostream Library


The `iostream` library provides input and output operations in C++.

Components:

● • `cin` – Standard input (keyboard)


● • `cout` – Standard output (console)
● • `cerr` – Standard error output
Example:

#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}

1.2 Comments in C++


Comments help in code documentation:

● • **Single-line:** `// This is a comment`


● • **Multi-line:** `/* This is a multi-line comment */`

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

1.3 Keywords in C++


Keywords are reserved words in C++ that cannot be used as variable names, such as: 95
keywords

● • `int`, `float`, `double`, `char`, `if`, `else`, `switch`, `for`, `while`, `return`, `class`, `public`,
`private`

1.4 Variable Declaration


Variables must be declared before use.

Example:

`int age = 25;`

`double price = 99.99;`

1.5 The const Qualifier


The `const` keyword defines constants whose values cannot be changed.

Example:

#include <iostream>
using namespace std;
int main() {
const int max_value = 100;
cout << "Max Value: " << max_value << endl;
return 0;
}

2. Control Structures in C++


Control structures determine the flow of execution in a program.

They are categorized into:

● • **Conditional Statements** (`if`, `if-else`, `switch`)


● • **Looping Statements** (`for`, `while`, `do-while`)
● • **Jump Statements** (`break`, `continue`, `goto`)

2.1 Conditional Statements

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

int num = 10;


if (num > 5) {
cout << "Number is greater than 5" << endl;
}
return 0;
}

2.1.2 if-else Statement


Executes one block if the condition is true and another if it is false.

Example:

#include <iostream>
using namespace std;
int main() {
int num = 3;
if (num % 2 == 0) {
cout << "Even number";
} else {
cout << "Odd number";
}
return 0;
}

2.1.3 switch Statement


Used for multi-way decision making.

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

2.2 Looping Statements

2.2.1 for Loop


Used when the number of iterations is known.

Example:

#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Iteration: " << i << endl;
}
return 0;
}

2.2.2 while Loop


Executes as long as the condition is true.

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

2.2.3 do-while Loop


Executes the code block at least once before checking the condition.

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++.

● • `endl` – Moves to a new line.


● • `setw(n)` – Sets the width of output.
● • `setprecision(n)` – Controls decimal precision.

Example:

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

double pi = 3.14159265;

cout << "Pi values:" << endl;

cout << setw(10) << "Default:" << pi << endl;

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

4. The Scope Resolution Operator (::)


Used to define a function outside its class or access global variables.

Example:

#include <iostream>

using namespace std;

int x = 10; // Global variable

int main() {

int x = 20; // Local variable

cout << "Local x: " << x << endl; // 20 (local)

cout << "Global x: " << ::x << endl; // 10 (global, using ::)

return 0;

5. The new and delete Operators


• `new` allocates memory dynamically.

• `delete` deallocates memory to prevent memory leaks.

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

6. Expressions and Implicit Conversions


Expressions involve variables and operators. Implicit conversions automatically change
data types.

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++

Functions in C++ allow code reusability and modular programming.

8.1 Function Prototyping

Function prototypes declare the function before its definition to ensure proper
compilation.

Example:

#include <iostream>

using namespace std;

int add(int x, int y); // Function prototype

int main() {

int sum = add(5,10); // Function call

cout<<”this is the sum of two numbers”<<sum<<endl;

return 0;

int add(int x, int y){ //Function defination

return x+y;

10
KLE BCA COLLEGE GANGAVATHI C++ using Oops

8.2 Call by Reference

Passing arguments by reference allows modifying the actual parameter.

Example:

#include <iostream>

using namespace std;

void swap(int &x, int &y) {

int temp = x;

x = y;

y = temp;

int main() {

int a = 5, b = 10;

swap(a, b);

cout << "a: " << a << ", b: " << b;

return 0;

11
KLE BCA COLLEGE GANGAVATHI C++ using Oops

8.3 Return by Reference

Returning a reference allows modifying the actual value.

Example:

#include <iostream>

using namespace std;

int& max(int &a, int &b) {

return (a > b) ? a : b;

int main() {

int x = 10, y = 20;

max(x, y) = 50; // Modifies the actual value

cout << "x: " << x << ", y: " << y;

return 0;

8.4 Inline Functions

Inline functions reduce function call overhead.

Example:

#include <iostream>

using namespace std;

inline int square(int x) {

return x * x;

int main() {

cout << "Square of 5: " << square(5);

return 0;

12
KLE BCA COLLEGE GANGAVATHI C++ using Oops

8.5 Default Arguments

Default values can be assigned to function parameters.

Example:

#include <iostream>

using namespace std;

void greet(string name = "Guest") {

cout << "Hello, " << name << "!" << endl;

int main() {

greet(); // Uses default value

greet("Alice"); // Overrides default value

return 0;

8.6 Const Arguments

Passing arguments as `const` prevents modifications inside the function.

Example:

#include <iostream>

using namespace std;

void display(const int &x) {

cout << "Value: " << x << endl;

int main() {

int num = 10;

display(num);

return 0;

13
KLE BCA COLLEGE GANGAVATHI C++ using Oops

8.7 Function Overloading

Function overloading allows multiple functions with the same name but different
parameters.

Example:

#include <iostream>

using namespace std;

void show(int x) {

cout << "Integer: " << x << endl;

void show(double x) {

cout << "Double: " << x << endl;

int main() {

show(10);

show(10.5);

return 0;

14

You might also like