0% found this document useful (0 votes)
7 views4 pages

OOSD Imp Program

The document contains various C++ programming concepts and examples, including classes, constructors, operator overloading, inheritance, virtual functions, and friend functions. Each section provides code snippets demonstrating the implementation of these concepts. Key topics covered include single and multiple inheritance, runtime polymorphism, pure virtual functions, and the use of static data members.

Uploaded by

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

OOSD Imp Program

The document contains various C++ programming concepts and examples, including classes, constructors, operator overloading, inheritance, virtual functions, and friend functions. Each section provides code snippets demonstrating the implementation of these concepts. Key topics covered include single and multiple inheritance, runtime polymorphism, pure virtual functions, and the use of static data members.

Uploaded by

madhavgoel890
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Class & Object (Basic Program)


#include <iostream>
using namespace std;

class Student {
int roll;
public:
void getData() {
cin >> roll;
}
void showData() {
cout << "Roll = " << roll;
}
};

int main() {
Student s;
[Link]();
[Link]();
return 0;
}

2. Constructor & Destructor


#include <iostream>
using namespace std;

class Demo {
public:
Demo() {
cout << "Constructor called\n";
}
~Demo() {
cout << "Destructor called\n";
}
};

int main() {
Demo obj;
return 0;
}

3. Parameterized Constructor
class Add {
int a, b;
public:
Add(int x, int y) {
a = x;
b = y;
}
void show() {
cout << a + b;
}
};

4. Function Overloading
#include <iostream>
using namespace std;

class Test {
public:
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
};

int main() {
Test t;
cout << [Link](2, 3) << endl;
cout << [Link](2.5, 3.5);
}

5. Operator Overloading (+)


#include <iostream>
using namespace std;

class Number {
int x;
public:
void set(int a) {
x = a;
}
Number operator + (Number obj) {
Number temp;
temp.x = x + obj.x;
return temp;
}
void show() {
cout << x;
}
};

int main() {
Number n1, n2, n3;
[Link](10);
[Link](20);
n3 = n1 + n2;
[Link]();
}

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

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

int main() {
B obj;
[Link]();
[Link]();
}
7. Multilevel Inheritance
class A {
public:
void showA() { cout << "A\n"; }
};

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

class C : public B {
public:
void showC() { cout << "C\n"; }
};

8. Multiple Inheritance
class A {
public:
void showA() { cout << "A\n"; }
};

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

class C : public A, public B {


};

9. Virtual Function (Runtime Polymorphism)


#include <iostream>
using namespace std;

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

class Derived : public Base {


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

int main() {
Base* b;
Derived d;
b = &d;
b->show();
}

10. Pure Virtual Function (Abstract Class)


class Shape {
public:
virtual void draw() = 0;
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle\n";
}
};

11. Friend Function


class Sample {
int x;
public:
void set(int a) {
x = a;
}
friend void show(Sample s);
};

void show(Sample s) {
cout << s.x;
}

12. Call by Reference


void swap(int &a, int &b) {
int t = a;
a = b;
b = t;
}

13. Static Data Member


class Test {
static int count;
public:
Test() {
count++;
}
void show() {
cout << count;
}
};

int Test::count = 0;

14. this Pointer


class Demo {
int x;
public:
void set(int x) {
this->x = x;
}
};

15. Inline Function


inline int square(int x) {
return x * x;
}

You might also like