0% found this document useful (0 votes)
53 views12 pages

OOPS (Mayank Sharma)

1. The document contains 6 code examples demonstrating different concepts in C++ including structs, classes, operator overloading, inheritance, and function pointers. 2. The examples show how to define and use structures to store student and employee data, define classes for complex number arithmetic and function calls, overload operators like + and ++, implement inheritance between super and base classes, and call methods using function pointers. 3. Common operations demonstrated include input/output using cout and cin, defining and accessing class members and methods, constructing and destructing objects, returning objects, and calling parent and derived class functions.

Uploaded by

Mayank Sharma
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)
53 views12 pages

OOPS (Mayank Sharma)

1. The document contains 6 code examples demonstrating different concepts in C++ including structs, classes, operator overloading, inheritance, and function pointers. 2. The examples show how to define and use structures to store student and employee data, define classes for complex number arithmetic and function calls, overload operators like + and ++, implement inheritance between super and base classes, and call methods using function pointers. 3. Common operations demonstrated include input/output using cout and cin, defining and accessing class members and methods, constructing and destructing objects, returning objects, and calling parent and derived class functions.

Uploaded by

Mayank Sharma
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
You are on page 1/ 12

1.

Name : Mayank Sharma


#include<iostream>
using namespace std;
struct detail
{
char city[10];
};
struct student
{
char name[10];
int id;
struct detail det;

};
void show(struct student s1[10], int num)
{
int i;
for(i=0;i<num;i++)
{
cout<<"\n\nName :"<<s1[i].name<<"\nId :"<<s1[i].id<<"\nCity :"<<s1[i].det.city;
}
}
int main()
{
int num,i;
struct student s[10];
cout<<"Number of Students :";
cin>>num;
for(i=0;i<num;i++)
{
cout<<"Enter Name : ";
cin>>s[i].name;
cout<<"Enter ID : ";
cin>>s[i].id;
cout<<"Enter City : ";
cin>>s[i].det.city;
}
show(s,num);

return(0);
}

OUTPUT :

2.
#include<iostream>
#include<stdio.h>
using namespace std;
struct employee
{
char name[20];
int salary;
};

void max(struct employee *e,int num)


{
int max=0,i,n;
for(i=0;i<num;i++)
{
if(e[i].salary>max)
{
max=e[i].salary;
n=i;
}
}
cout<<"\nName : "<<e[n].name;
cout<<"\nSalary : "<<e[n].salary;

}
void enter(int num)
{
struct employee e[20];
int i;
for(i=0;i<num;i++)
{
cout<<"Enter Name :";
cin>>e[i].name;
cout<<"Enter Salary :";
cin>>e[i].salary;
}
max(e,num);
}
int main()
{
int num;
cout<<"how Many employee :";
cin>>num;
enter(num);
return(0);
}
OUTPUT :

3.
#include <iostream>
using namespace std;
class complex
{
int real, imag;

public:
complex()
{
real = 0;
imag = 0;
}
complex(int r, int i)

{
real = r;
imag = i;
}
complex(complex &c)
{
real = c.real;
imag = c.imag;
}
void getData()
{
cout << "\nthe complex number are: " << real << "+" << imag << "i\n";
}
void showData()
{
cout << "\nthe sum is: ";
cout << real << "+" << imag << "i" << endl;
}

complex operator+(complex &p)


{
complex temp;
temp.real = real + p.real;
temp.imag = imag + p.imag;
return (temp);
}
};

int main()
{
complex c1, c2(2, 3), c3(6, 8), c4(c3);
c2.getData();
c4.getData();
c1 = c2 + c3;
c1.showData();
return 0;
}
OUTPUT :

4.
#include <iostream>
using namespace std;
class data
{
public:
int i, j,result;
data(int i1,int j1)
{
i=i1;
j=j1;
}
void add()
{
result = i+j;
cout<<"\nAddition :"<<result;
}
void mul()
{
result = i * j;
cout<<"\nMultiply :"<<result;
}
};
int main()
{
data d1(10,2), d2(5,6);
void (data::*p2)();
void (data::*p3)();
p2 = &data::add;
p3 = &data::mul;
(d1.*p2)();
(d2.*p3)();
return(0);
}
OUTPUT :

5.
#include <iostream>
using namespace std;
class complex
{
public:
int real, imag;
complex()
{
real = 0;
imag = 0;
}
complex(int r, int i)
{
real = r;
imag = i;
}
complex operator+(complex &p)
{
complex temp;
temp.real = real + p.real;
temp.imag = imag + p.imag;
return (temp);
}
complex operator++()
{
complex temp;
temp.real = ++real;
temp.imag = ++imag;
return temp;
}
complex operator++(int)
{
complex temp1;
temp1.real = real++;
temp1.imag = imag++;
return temp1;
}
void print()
{
cout << "\nReal Part :" << real;
cout << "\nImag Part:" << imag;
}
};
int main()
{
complex c1(5, 3), c2(8, 5), c3, c4;
c3 = c1 + c2;
c3.print();
c4 = ++c1;
c4.print();
c4 = c2++;
c4.print();
return (0);
}
OUTPUT :

6.(Inheritance)
#include<iostream>
using namespace std;
class super
{
protected:
int num=5;
public:
void print()
{
cout<<"Hello";
}
super()
{
cout<<"\nConstructor of Super Class\n";
}
~super()
{
cout<<"\nDesructor Of super class\n";
}
};
class base: public super
{
public:
base()
{
cout<<"\nConstructor of Base Class\n";
}
~base()
{
cout<<"\nDesructor Of Base class\n";
}
void show()
{
cout<<"\nNumber is :"<<num;
}
};
int main()
{
base b;
b.print();
b.show();
return(0);
}
OUTPUT :
Signature :

You might also like