0% found this document useful (0 votes)
4 views8 pages

Organized C++ Programs

The document outlines a C++ programming course divided into four units covering Object-Oriented Programming (OOP), control structures, classes, inheritance, polymorphism, templates, and file I/O. Each unit includes code examples demonstrating key concepts such as classes, static variables, memory management, functions, and exception handling. The course aims to provide a comprehensive understanding of C++ programming through practical coding exercises.

Uploaded by

Amber Gupta
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)
4 views8 pages

Organized C++ Programs

The document outlines a C++ programming course divided into four units covering Object-Oriented Programming (OOP), control structures, classes, inheritance, polymorphism, templates, and file I/O. Each unit includes code examples demonstrating key concepts such as classes, static variables, memory management, functions, and exception handling. The course aims to provide a comprehensive understanding of C++ programming through practical coding exercises.

Uploaded by

Amber Gupta
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/ 8

C++ Programs - Course Outline

Unit I: Introduction to OOP and Basics

Unit I - Introduction to OOP

// Basic OOP with class and object


#include<iostream>
using namespace std;

class Car {
public:
string brand;
int year;
void show() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};

int main() {
Car c;
c.brand = "Toyota";
c.year = 2020;
c.show();
return 0;
}

Unit I - Static and Constant Variables

// Demonstrate static and const in C++


#include<iostream>
using namespace std;

class Demo {
public:
static int count;
const int id;
Demo(int i) : id(i) {
count++;
}
void show() {
cout << "ID: " << id << ", Count: " << count << endl;
}
};
int Demo::count = 0;

int main() {
Demo d1(101), d2(102);
d1.show();
d2.show();
return 0;
}
C++ Programs - Course Outline

Unit I - Scope Resolution Operator

// Using scope resolution operator


#include<iostream>
using namespace std;

int val = 100;

int main() {
int val = 200;
cout << "Local val: " << val << endl;
cout << "Global val: " << ::val << endl;
return 0;
}

Unit I - Memory Management (new/delete)

// Dynamic memory allocation


#include<iostream>
using namespace std;

int main() {
int* p = new int(10);
cout << "Value: " << *p << endl;
delete p;
return 0;
}

Unit I - Type Conversion and Reference Variables

// Type casting and reference


#include<iostream>
using namespace std;

int main() {
double x = 10.5;
int y = static_cast<int>(x);
cout << "After casting: " << y << endl;

int a = 5;
int& ref = a;
ref = 10;
cout << "a: " << a << ", ref: " << ref << endl;
return 0;
}
C++ Programs - Course Outline

Unit II: Control Structures, Arrays, Strings, Functions

Unit II - Control Structures

// Demonstrating control structures


#include<iostream>
using namespace std;

int main() {
int i = 0;
while(i < 5) {
if(i == 3) {
i++;
continue;
}
cout << "i = " << i << endl;
i++;
}
return 0;
}

Unit II - Arrays and Strings

// Working with arrays and strings


#include<iostream>
#include<string>
using namespace std;

int main() {
int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++)
cout << arr[i][j] << " ";
cout << endl;
}

string str = "Hello";


str += " World";
cout << str << endl;
return 0;
}

Unit II - Pointers

// Pointer example
#include<iostream>
using namespace std;

int main() {
C++ Programs - Course Outline

int a = 10;
int* p = &a;
cout << "Value: " << *p << ", Address: " << p << endl;
return 0;
}

Unit II - Functions in C++

// Inline and Friend functions


#include<iostream>
using namespace std;

class B;
class A {
int x;
public:
A() : x(10) {}
friend void show(A, B);
};

class B {
int y;
public:
B() : y(20) {}
friend void show(A, B);
};

void show(A a, B b) {
cout << "Sum: " << a.x + b.y << endl;
}

inline int cube(int x) {


return x*x*x;
}

int main() {
A a;
B b;
show(a, b);
cout << "Cube of 3: " << cube(3) << endl;
return 0;
}
C++ Programs - Course Outline

Unit III: Classes, Objects, Constructors and Destructors

Unit III - Classes and Objects

// Class vs structure and access specifiers


#include<iostream>
using namespace std;

class Box {
int length;
public:
void setLength(int l) { length = l; }
int getLength() { return length; }
};

int main() {
Box b;
b.setLength(10);
cout << "Length: " << b.getLength() << endl;
return 0;
}

Unit III - Constructors and Destructors

// Constructors and destructors


#include<iostream>
using namespace std;

class Test {
public:
Test() { cout << "Default Constructor\n"; }
Test(int x) { cout << "Parameterized Constructor: " << x << endl; }
~Test() { cout << "Destructor called\n"; }
};

int main() {
Test t1, t2(5);
return 0;
}
C++ Programs - Course Outline

Unit IV: Inheritance, Polymorphism, Templates, Exception Handling

Unit IV - Inheritance

// Demonstrating inheritance types


#include<iostream>
using namespace std;

class A {
public:
void showA() { cout << "Class A\n"; }
};

class B : public A {
public:
void showB() { cout << "Class B\n"; }
};

int main() {
B b;
b.showA();
b.showB();
return 0;
}

Unit IV - Polymorphism & Overloading

// Function overloading and overriding


#include<iostream>
using namespace std;

class Base {
public:
virtual void show() { cout << "Base class\n"; }
};

class Derived : public Base {


public:
void show() override { cout << "Derived class\n"; }
};

void display(int x) { cout << "Integer: " << x << endl; }


void display(double x) { cout << "Double: " << x << endl; }

int main() {
Base* b = new Derived();
b->show();

display(10);
display(5.5);
C++ Programs - Course Outline

return 0;
}

Unit IV - Templates and Exception Handling

// Templates and exceptions


#include<iostream>
using namespace std;

template<typename T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << "Sum: " << add<int>(3, 4) << endl;
try {
throw "An error occurred";
} catch(const char* msg) {
cout << "Caught: " << msg << endl;
}
return 0;
}
C++ Programs - Course Outline

File I/O: File Input and Output in C++

File I/O - Open, Write, Read, Close

// File I/O in C++


#include<iostream>
#include<fstream>
using namespace std;

int main() {
ofstream out("sample.txt");
out << "Hello File!" << endl;
out.close();

ifstream in("sample.txt");
string line;
while(getline(in, line))
cout << line << endl;
in.close();
return 0;
}

You might also like