Organized C++ Programs
Organized C++ Programs
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;
}
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
int main() {
int val = 200;
cout << "Local val: " << val << endl;
cout << "Global val: " << ::val << endl;
return 0;
}
int main() {
int* p = new int(10);
cout << "Value: " << *p << endl;
delete p;
return 0;
}
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
int main() {
int i = 0;
while(i < 5) {
if(i == 3) {
i++;
continue;
}
cout << "i = " << i << endl;
i++;
}
return 0;
}
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;
}
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;
}
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;
}
int main() {
A a;
B b;
show(a, b);
cout << "Cube of 3: " << cube(3) << endl;
return 0;
}
C++ Programs - Course Outline
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;
}
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
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;
}
class Base {
public:
virtual void show() { cout << "Base class\n"; }
};
int main() {
Base* b = new Derived();
b->show();
display(10);
display(5.5);
C++ Programs - Course Outline
return 0;
}
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
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;
}