15 Moderate C++ Programs Using Classes and Objects
15 Moderate C++ Programs Using Classes and Objects
class Dog {
public:
void bark() {
cout << "Woof!" << endl;
}
};
int main() {
Dog myDog;
myDog.bark();
return 0;
}
2. Constructor Example
#include <iostream>
using namespace std;
class Car {
public:
Car() {
cout << "Car created!" << endl;
}
};
int main() {
Car myCar;
return 0;
}
class Rectangle {
public:
int width;
int height;
int area() {
return width * height;
}
};
int main() {
Rectangle rect;
rect.width = 5;
rect.height = 10;
cout << "Area: " << rect.area() << endl;
return 0;
}
4. Parameterized Constructor
#include <iostream>
using namespace std;
class Box {
public:
Box(int l, int w, int h) {
length = l;
width = w;
height = h;
}
int volume() {
return length * width * height;
}
private:
int length, width, height;
};
int main() {
Box box(3, 4, 5);
cout << "Volume: " << box.volume() << endl;
return 0;
}
5. Copy Constructor
#include <iostream>
using namespace std;
class Point {
public:
int x, y;
int main() {
Point p1(1, 2);
Point p2 = p1; // Copy constructor
cout << "Point 2: (" << p2.x << ", " << p2.y << ")" << endl;
return 0;
}
6. Inheritance
#include <iostream>
using namespace std;
class Animal {
public:
void speak() {
cout << "Animal sound" << endl;
}
};
7. Method Overloading
#include <iostream>
using namespace std;
class Display {
public:
void show(int i) {
cout << "Integer: " << i << endl;
}
void show(double d) {
cout << "Double: " << d << endl;
}
};
int main() {
Display d;
d.show(10);
d.show(10.5);
return 0;
}
8. Access Specifiers
#include <iostream>
using namespace std;
class Person {
private:
string name;
public:
void setName(string n) {
name = n;
}
string getName() {
return name;
}
};
int main() {
Person p;
p.setName("Alice");
cout << "Name: " << p.getName() << endl;
return 0;
}
class Counter {
public:
static int count;
Counter() {
count++;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
cout << "Count: " << Counter::count << endl;
return 0;
}
class Box {
private:
int width;
public:
Box(int w) : width(w) {}
friend void printWidth(Box b);
};
void printWidth(Box b) {
cout << "Width: " << b.width << endl;
}
int main() {
Box box(10);
printWidth(box);
return 0;
}
class Complex {
public:
int real, imag;
int main() {
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
cout << "Sum: " << c3.real << " + " << c3.imag << "i" << endl;
return 0;
}
class Outer {
public:
class Inner {
public:
void display() {
cout << "Inner class method called." << endl;
}
};
};
int main() {
Outer::Inner innerObj;
innerObj.display();
return 0;
}
class Number {
public:
int value;
Number(int v) : value(v) {}
void show() {
cout << "Value: " << this->value << endl;
}
};
int main() {
Number num(5);
num.show();
return 0;
}
14. Class with Default Arguments
#include <iostream>
using namespace std;
class Rectangle {
public:
int width, height;
int area() {
return width * height;
}
};
int main() {
Rectangle rect;
cout << "Area: " << rect.area() << endl;
return 0;
}
15. Using Constructors and Destructors
#include <iostream>
using namespace std;
class Book {
public:
Book() {
cout << "Book created!" << endl;
}
~Book() {
cout << "Book destroyed!" << endl;
}
};
int main() {
Book myBook;
return 0;
}