0% found this document useful (0 votes)
4 views17 pages

OOP (Programs)

The document provides a series of C++ code examples illustrating fundamental programming concepts such as classes, objects, constructors, destructors, function overloading, friend functions, inheritance, inline functions, operator overloading, and file handling. Each section includes code snippets followed by their expected output. The examples demonstrate practical applications of these concepts in a clear and concise manner.

Uploaded by

rahul159806
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)
4 views17 pages

OOP (Programs)

The document provides a series of C++ code examples illustrating fundamental programming concepts such as classes, objects, constructors, destructors, function overloading, friend functions, inheritance, inline functions, operator overloading, and file handling. Each section includes code snippets followed by their expected output. The examples demonstrate practical applications of these concepts in a clear and concise manner.

Uploaded by

rahul159806
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

Class and Object

#include
<iostream>
#include<string>

using namespace std;

class Student {
public:
name;
int
age;

void input() {
cin >> name >> age;
}

void display() {
cout << name << " " << age;
}

};

int main() {

Student s;
[Link]();
[Link]()
; return 0;

Output:

Abhay 20
CONSTRUCTOR & DESTRUCTOR

#include
<iostream>
#include<string>

using namespace std;

class Demo {
public:
Demo() {
cout << "Constructor Called\n";
}

~Demo() {
cout << "Destructor Called\n";
}

};

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

Output:

Constructor
Called Destructor
Called
FUNCTION OVERLOADING

#include
<iostream>
#include<string>

using namespace std;

class Add {
public:
int sum(int a, int b) {
return a + b;
}

double sum(double a, double


b) { return a + b;
}

};

int

main()

{ Add

obj;
cout << [Link](5, 10) <<
endl; cout << [Link](2.5,
3.5);

Output:

15
6
FRIEND FUNCTION

#include
<iostream>
#include<string>

using namespace std;

class Test {
private:
int a, b;

public:
Test() {
a = 10;
b = 20;
}

friend int sum(Test t);


};

int sum(Test t)
{ return t.a +
t.b;
}

int main() {

Test obj;
cout << "Sum = " <<
sum(obj); return 0;

Output:

Sum = 30
FRIEND CLASS

#include
<iostream>
#include<string>

using namespace std;

class A {
private
:
int x;

public:
A() {
x = 50;
}

friend class B;
};

class B {
public:
void show(A obj) {
cout << "Value = " << obj.x;
}

};

int main() {
A a;
B b;
[Link](a);
return 0;
}

Output:

Value = 50
SINGLE INHERITANCE

#include
<iostream>
#include<string>
using namespace
std;

class A {
public:
void showA() {
cout << "Inside A\n";
}
};

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

int
main()
{B
obj;
[Link]();
[Link]();
return 0;
}

Output:

Inside
A
Inside
B
INLINE FUNCTION

#include
<iostream>
#include <string>
using namespace
std;

class Marks
{ private:
int math, science, english;

public:
inline void input() {
cout << "Enter Maths
Marks: "; cin >> math;
cout << "Enter Science
Marks: "; cin >> science;
cout << "Enter English
Marks: "; cin >> english;
}

inline int total() {


return math + science + english;
}

inline float average()


{ return total() /
3.0;
}

inline void display() {


cout << "\n----- RESULT--\n";
cout << "Maths: " << math <<
endl; cout << "Science: " <<
science << endl; cout <<
"English: " << english << endl;
cout << "Total Marks: " << total() <<
endl; cout << "Average: " <<
average() << endl;
}
};

int main() {
Marks m;
[Link]();
[Link](
); return
0;
}

Output

Maths: 80
Science: 75
English: 90
Total Marks: 245
Average: 81.66
MULTILEVEL INHERITANCE

#include
<iostream>
#include<string>

using namespace std;

class Person {
protected:
string name;
public:
void setName(string
n) { name = n;
}
};

class Student : public Person {


protected:
int roll;
public:
void setRoll(int r)
{ roll = r;
}
};

class Marks : public Student {


private:
int m1, m2,
m3; public:
void setMarks(int a, int b, int
c) { m1 = a;
m2 = b;
m3 = c;
}

void display() {
cout << "\n----- STUDENT RESULT- \n";
cout << "Name: " << name <<
endl; cout << "Roll No: " << roll
<< endl;
cout << "Marks: " << m1 << ", " << m2 << ", " <<
m3 << endl; cout << "Total = " << (m1 + m2 + m3)
<< endl;
}
};

int main()
{ Marks
obj;
[Link]("Abhay");
[Link](101);
[Link](85, 90, 88);
[Link]();
return 0;
}

Output:

----- STUDENT RESULT -----


Name:
Abhay Roll
No: 101
Marks: 85, 90, 88
Total = 263
OPERATOR OVERLOADING (+ OPERATOR)

#include
<iostream>
#include<string>

using namespace std;

class Complex {
private:
int
real;
int
imag;

public:
Complex()
{ real =
0;
imag = 0;
}

Complex(int r, int i)
{ real = r;
imag = i;
}

// Operator Overloading
Complex operator + (Complex
c) { Complex temp;
[Link] = real + [Link];
[Link] = imag +
[Link]; return temp;
}

void display() {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(5, 4);
Complex c2(3, 6);

cout << "First Number: ";


[Link]();

cout << "Second Number: ";


[Link]();

Complex result = c1 + c2;

cout << "Addition =


"; [Link]();

return 0;
}

Output:

First Number: 5 + 4i
Second Number: 3 + 6i
Addition = 8 + 10i
FILE HANDLING (WRITE + READ FILE)

#include
<iostream>
#include
<fstream>
#include<string>

using namespace

std; int main() {


ofstream fout;
[Link]("[Link]
t"); string name;
int roll;

cout << "Enter


Name: "; cin >>
name;

cout << "Enter Roll


No: "; cin >> roll;

fout << "Name: " << name


<< endl; fout << "Roll No: "
<< roll << endl;

[Link]();

// Reading the file


ifstream
fin("[Link]");
string line;

cout << "\n----- FILE CONTENT--\n";

while (getline(fin,
line)) { cout <<
line << endl;
}

[Link]();
return 0;
}

Output:

----- FILE CONTENT -----


Name:
Abhay Roll
No: 101

You might also like