Programming in C
Programming in C
✅ Advantages
1. Easier to Read and Understand
o Clean structure and modular design make it simple to follow.
2. Easier to Debug and Maintain
o Isolated modules allow easier error detection and updates.
3. Reusability
o Functions or modules can be reused in other programs.
4. Improves Productivity
o Speeds up development and reduces effort with well-organized code.
5. Encourages Discipline
o Promotes good coding habits and logical thinking.
❌ Disadvantages
1. Not Suitable for All Problems
o Complex real-world scenarios may not fit well into a strictly structured model.
2. Limited Support for Real-World Modeling
o Cannot represent objects or entities as naturally as object-oriented
programming.
3. Code Duplication
o Without classes/objects, data and functions may be repeated in different parts.
4. Harder to Modify Large Programs
o Changes in one module might affect others, especially without proper design.
🔚 Conclusion
Structured programming is great for small to medium-sized applications where clarity
and logic are important. However, for complex systems, object-oriented programming
(OOP) is often more effective.
2.Write a C++ program to demonstrate the use of switch statement.
include <iostream>
using namespace std;
int main() {
int num1, num2, choice;
float result;
// Display menu
cout << "Simple Calculator using switch statement\n";
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "Enter your choice (1-4): ";
cin >> choice;
// Switch statement
switch(choice) {
case 1:
result = num1 + num2;
cout << "Result = " << result << endl;
break;
case 2:
result = num1 - num2;
cout << "Result = " << result << endl;
break;
case 3:
result = num1 * num2;
cout << "Result = " << result << endl;
break;
case 4:
if (num2 != 0)
result = (float)num1 / num2;
else {
cout << "Error: Division by zero!" << endl;
return 1;
}
cout << "Result = " << result << endl;
break;
default:
cout << "Invalid choice!" << endl;
}
return 0;
}
🧪 Sample Output:
cpp
Copy
Edit
Simple Calculator using switch statement
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 3
Enter two numbers: 5 6
Result = 30
3.What is an object? Explain how objects in C++ are created and
destroyed, with help of example
🔷 What is an Object in C++?
In C++, an object is an instance of a class.
A class is like a blueprint, and an object is a real-world entity created based on that
blueprint.
Example: If Car is a class, then myCar and yourCar are objects of that class.
Objects encapsulate data and functions that operate on that data.
class Demo {
public:
Demo() { cout << "Object created\n"; }
~Demo() { cout << "Object destroyed\n"; }
};
int main() {
Demo obj; // Static object created
return 0; // Destructor called automatically here
}
Output:
css
Copy
Edit
Object created
Object destroyed
4.list any 5common examples of exception
Here are 5 common examples of exceptions in programming (especially in C++ or other
high-level languages):
✅ Common Exceptions:
1. Divide by Zero Exception
o Occurs when a number is divided by zero.
o Example: int x = 5 / 0;
2. Null Pointer Exception
o Trying to access or use a pointer that is null.
o Example: int* p = nullptr; *p = 10;
3. Array Index Out of Bounds
o Accessing an element outside the valid range of an array.
o Example: arr[10] when array size is 5.
4. Invalid Input/Type Conversion
o Trying to convert a string to a number when it’s not valid.
o Example: stoi("abc") in C++ throws invalid_argument.
5. File Not Found Exception
o Occurs when trying to open a file that does not exist.
o Example: ifstream file("nonexistent.txt");
🔧 Example:
cpp
Copy code
class Car {
public:
Car() {
cout << "Car created!" << endl;
}
};
int main() {
Car myCar; // Constructor is automatically called
return 0;
}
🔧 Example:
cpp
Copy code
class Car {
private:
int speed;
public:
void setSpeed(int s) {
speed = s;
}
void displaySpeed() {
cout << "Speed: " << speed << endl;
}
};
int main() {
Car myCar;
myCar.setSpeed(80);
myCar.displaySpeed();
return 0;
}
✅ 4(a) Operator Overloading in C++
🔹 Need for Operator Overloading
Operator overloading allows *standard operators (like +, -, , etc.) to work with user-defined
types (like classes).
🧠 Why It's Needed:
To make class objects behave like built-in types
Improves code readability and reusability
Allows intuitive usage of objects in expressions
class Complex {
private:
float real, imag;
public:
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
}
// Overload + operator
Complex operator + (Complex c) {
return Complex(real + c.real, imag + c.imag);
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(2.5, 3.5), c2(1.5, 2.0);
Complex c3 = c1 + c2; // uses overloaded + operator
return 0;
}
🔹 Multiple Inheritance
In multiple inheritance, a class can inherit from more than one base class.
class A {
public:
void showA() {
cout << "Class A function\n";
}
};
class B {
public:
void showB() {
cout << "Class B function\n";
}
};
int main() {
C obj;
obj.showA(); // from class A
obj.showB(); // from class B
obj.showC(); // from class C
return 0;
}
✅ Summary:
Concept Purpose
Multiple Inheritance Derive a class from more than one base class
class Box {
private:
int length;
public:
Box() { length = 0; }
int main() {
Box b1;
b1.setLength(10);
printLength(b1); // accessing private member using friend function
return 0;
}
🧾 Output:
rust
Copy code
Length of box: 10
📌 Summary Table:
Characteristic Description
class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound\n";
}
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks\n";
}
};
int main() {
Animal* a;
Dog d;
a = &d;
return 0;
}
🧾 Output:
nginx
Copy code
Dog barks
✅ Summary:
Concept Description
2. 🔸 Objects
An object is an instance of a class.
It represents a real-world entity like a student, car, or book.
Objects contain data (attributes) and functions (methods) to operate on that data.
3. 🔸 Structured Programming
A programming paradigm that emphasizes clear, linear flow of control.
Uses sequences, selections (if/switch), and loops.
Makes code easier to read, debug, and maintain.
4. 🔸 Access Specifier
Defines the visibility/scope of class members.
Types:
o private – Accessible only within the class
o public – Accessible from anywhere
o protected – Accessible in the class and its derived classes
Controls how data and functions are accessed and modified.
5. 🔸 Class
A class is a blueprint or template for creating objects.
It defines data members (variables) and member functions (methods).
Supports concepts like encapsulation, inheritance, and abstraction.