0% found this document useful (0 votes)
27 views3 pages

C++ Basics and Advance

The document covers fundamental C++ concepts including input/output, variables, functions, control flow, arrays, pointers, classes, inheritance, polymorphism, and exception handling. It provides code examples demonstrating function overloading, templates, and the use of the Standard Template Library (STL) for sorting vectors. Additionally, it includes instructions on how to compile and run the provided code.

Uploaded by

noxadi3388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

C++ Basics and Advance

The document covers fundamental C++ concepts including input/output, variables, functions, control flow, arrays, pointers, classes, inheritance, polymorphism, and exception handling. It provides code examples demonstrating function overloading, templates, and the use of the Standard Template Library (STL) for sorting vectors. Additionally, it includes instructions on how to compile and run the provided code.

Uploaded by

noxadi3388
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

✅ Concepts Covered:

iostream and basic I/O

Variables and data types

Functions and overloading

Control flow: if, switch, for, while

Arrays and vectors

Pointers and references

Classes, constructors, and destructors

Inheritance and polymorphism

STL: vector, sort

Exception handling

Templates

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdexcept>
using namespace std;

// Function with overloading


int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }

// Template function
template <typename T>
T maxVal(T a, T b) { return (a > b) ? a : b; }

// Base class
class Person {
public:
string name;
Person(string n) : name(n) {}
virtual void greet() { cout << "Hello, I'm " << name << endl; }
virtual ~Person() {}
};

// Derived class
class Student : public Person {
public:
int grade;
Student(string n, int g) : Person(n), grade(g) {}
void greet() override {
cout << "Hi, I'm " << name << " and I'm in grade " << grade << endl;
}
};

int main() {
// Variables and input
int a = 5, b = 10;
cout << "Sum of " << a << " + " << b << " = " << add(a, b) << endl;
cout << "Max(3.14, 2.71) = " << maxVal(3.14, 2.71) << endl;

// Conditional statements
if (a < b) cout << "a is less than b\n";

// Switch case
int x = 2;
switch (x) {
case 1: cout << "One\n"; break;
case 2: cout << "Two\n"; break;
default: cout << "Other\n";
}

// Arrays and loops


int arr[3] = {10, 20, 30};
for (int i = 0; i < 3; i++) cout << "arr[" << i << "] = " << arr[i] << endl;

// Vectors and STL


vector<int> nums = {5, 2, 8, 1};
sort(nums.begin(), nums.end());
cout << "Sorted vector: ";
for (int n : nums) cout << n << " ";
cout << endl;

// While loop
int count = 3;
while (count--) cout << "Countdown: " << count << endl;

// Pointers and references


int val = 42;
int* p = &val;
int& ref = val;
cout << "Pointer: " << *p << ", Reference: " << ref << endl;

// Object and polymorphism


Person* p1 = new Person("Alice");
Person* p2 = new Student("Bob", 10);
p1->greet();
p2->greet();
delete p1;
delete p2;

// Exception handling
try {
int denom = 0;
if (denom == 0) throw runtime_error("Division by zero!");
cout << 10 / denom;
} catch (const exception& e) {
cout << "Caught error: " << e.what() << endl;
}

return 0;
}
| Feature | Example Line |
| --------------------- | -------------------------------- |
| Input/Output | `cout <<`, `cin >>` |
| Functions & Templates | `add()`, `maxVal<T>()` |
| Control Flow | `if`, `switch`, `for`, `while` |
| Arrays & STL Vectors | `int arr[]`, `vector<int>` |
| Pointers & References | `int* p`, `int& ref` |
| Classes & Inheritance | `Person`, `Student` |
| Polymorphism | `virtual greet()` |
| Exception Handling | `try-catch` with `runtime_error` |

🛠 How to Run
Save as main.cpp, compile and run:

bash
Copy code
g++ -std=c++17 main.cpp -o main
./main

You might also like