0% found this document useful (0 votes)
2 views

Assignment oosd

The document contains multiple C++ code examples demonstrating various programming concepts including virtual functions, this pointer, dangling pointers, constructors and destructors, function overloading, operator overloading, inline functions, friend functions, multilevel inheritance, multiple inheritance, and polymorphism. Each section includes code snippets and their expected outputs. These examples serve as educational material for understanding fundamental C++ programming principles.

Uploaded by

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

Assignment oosd

The document contains multiple C++ code examples demonstrating various programming concepts including virtual functions, this pointer, dangling pointers, constructors and destructors, function overloading, operator overloading, inline functions, friend functions, multilevel inheritance, multiple inheritance, and polymorphism. Each section includes code snippets and their expected outputs. These examples serve as educational material for understanding fundamental C++ programming principles.

Uploaded by

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

1:Virtual Function

Code:
#include <iostream>

using namespace std;

class Base {

public:

virtual void print() {

cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1;

Base* base1 = &derived1;

base1->print();

return 0; }

Output:
2: This Pointer
Code:
#include <iostream>

using namespace std;

class Employee {

public:

int id;

string name;

float salary;

Employee(int id, string name, float salary) {

this->id = id;

this->name = name;

this->salary = salary; }

void display() {

cout<<id<<" "<<name<<" "<<salary<<endl; }

};

int main(void) {

Employee e1 =Employee(101, "XX", 890000);

Employee e2=Employee(102, "yy", 59000);

e1.display();

e2.display();

return 0; }

Output:

3: Dangling Pointer
Code:
#include<iostream>

using namespace std;

int *fun()

{
int x = 10;

return &x;

int main() {

int *p = fun();

fflush(stdin);

cout<< *p;

return 0;

OUTPUT:

4:Constructor and Destructor


Code:
#include<iostream>

using namespace std;

class Test

{
public:

Test()

cout<<"\n Constructor executed";

~Test()

cout<<"\n Destructor executed";

};

main()

Test t;

return 0;

OUTPUT:

5: Function Overloading
Code:
#include <iostream>

using namespace std;

void add(int a, int b)


{

cout << "sum = " << (a + b);

void add(double a, double b)

cout << endl << "sum = " << (a + b);

int main()

add(10, 2);

add(5.3, 6.2);

return 0;

OUTPUT:

6: Operator Overloading
Code:
#include <iostream>

using namespace std;

class Complex {

private:
float real;

float imag;

public:

Complex() : real(0), imag(0) {}

void input() {

cout << "Enter real and imaginary parts respectively: ";

cin >> real;

cin >> imag;

Complex operator + (const Complex& obj) {

Complex temp;

temp.real = real + obj.real;

temp.imag = imag + obj.imag;

return temp;

void output() {

if (imag < 0)

cout << "Output Complex number: " << real << imag << "i";

else

cout << "Output Complex number: " << real << "+" << imag << "i";

};

int main() {

Complex complex1, complex2, result;

cout << "Enter first complex number:\n";

complex1.input();

cout << "Enter second complex number:\n";

complex2.input();

result = complex1 + complex2;

result.output();

return 0;
}

OUTPUT:

7: (i) Inline Function:


Code:
#include <iostream>

using namespace std;

inline int add(int a, int b)

{
return(a+b);

int main()

cout<<"Addition of 'a' and 'b' is:"<<add(2,3);A

return 0;

OUTPUT:

(ii) Friend Function:


Code:
#include <iostream>

using namespace std;

class Box

{
private:

int length;

public:

Box(): length(0) { }

friend int printLength(Box); //friend function

};

int printLength(Box b)

b.length += 10;

return b.length;

int main()

Box b;

cout<<"Length of box: "<< printLength(b)<<endl;

return 0; }

OUTPUT:

8: Multilevel Inheritance
Code:
#include<bits/stdc++.h>

using namespace std;

class Vehicle{

public:
Vehicle(){

cout<<"This is a vehicle"<<endl; }

};

class fourWheeler: public Vehicle{

public:

fourWheeler(){

cout<<"Objects with 4 wheels are vehicles"<<endl; }

};

class Car: public fourWheeler{

public:

car(){

cout<<"Car has 4 Wheels"<<endl; }

};

int main(){

Car obj;

return 0; }

OUTPUT:

9: Multiple Inheritance
Code:
#include <iostream>

using namespace std;

class Mammal {

public:

Mammal() {
cout << "Mammals can give direct birth." << endl;

};

class WingedAnimal {

public:

WingedAnimal() {

cout << "Winged animal can flap." << endl;

};

class Bat: public Mammal, public WingedAnimal {};

int main() {

Bat b1;

return 0; }

OUTPUT:

10: Polymorphism
Code:
#include <iostream>

using namespace std;

class Base {

public:

void print() {
cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1;

derived1.print();

return 0;

OUTPUT:

You might also like