Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

15 Moderate C++ Programs Using Classes and Objects

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

15 Moderate C++ Programs Using Classes and Objects

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

15 Basic C++ Program using Classes and objects

1. Simple Class Definition


#include <iostream>
using namespace std;

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;
}

3. Class with Attributes


#include <iostream>
using namespace std;

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;

Point(int a, int b) : x(a), y(b) {}


Point(const Point &p) : x(p.x), y(p.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;
}
};

class Dog : public Animal {


public:
void speak() {
cout << "Woof!" << endl;
}
};
int main() {
Dog myDog;
myDog.speak();
return 0;
}

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;
}

9. Static Member Variable


#include <iostream>
using namespace std;

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;
}

10. Friend Function


#include <iostream>
using namespace std;

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;
}

11. Operator Overloading


#include <iostream>
using namespace std;

class Complex {
public:
int real, imag;

Complex(int r, int i) : real(r), imag(i) {}

Complex operator + (const Complex &c) {


return Complex(real + c.real, imag + c.imag);
}
};

int main() {
Complex c1(1, 2), c2(3, 4);
Complex c3 = c1 + c2;
cout << "Sum: " << c3.real << " + " << c3.imag << "i" << endl;
return 0;
}

12. Nested Classes


#include <iostream>
using namespace std;

class Outer {
public:
class Inner {
public:
void display() {
cout << "Inner class method called." << endl;
}
};
};

int main() {
Outer::Inner innerObj;
innerObj.display();
return 0;
}

13. Using this Pointer


#include <iostream>
using namespace std;

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;

Rectangle(int w = 5, int h = 10) : width(w), height(h) {}

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;
}

You might also like