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

Bca 3

Uploaded by

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

Bca 3

Uploaded by

Rakesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Page |1

RAJA BALWANT SINGH COLLEGE,


AGRA
SESSION 2018-19

Assignment
Of
Object Oriented
Programming Using 'C++'
Submitted To:
Mr. Vivek Sir Submitted By:
Utkarsh Jindal
BCA 3rd Sem
Page |2

Acknowledgement
I would like to express my special thanks of
gratitude to my teacher Mr. VIVEK SIR who
gave me the golden opportunity to do this
wonderful project on the topic OBJECT
ORIENTED PROGRAMMING USING 'C++' ,
which also helped me in doing a lot of Research
and i came to know about so many new things I
am really thankful to them.
Secondly i would also like to thank my parents
and friends who helped me a lot in finalizing this
project within the limited time frame.
Page |3

INDEX
PROGRAM PAGE NO.

Demo Class 4
Default Argument 6
Function Overloading 7
Inline Function 9
Static Data Member & Functions 10
Friend Function 12
Unary Operator Overloading 14
Binary Operator Overloading 16
Constructor 19
Virtual Function 21
Exceptional Handling 23
Namespace 25
Manipulators 27
Inheritance 28
Generic Function 31
Generic Classes 32
Writing in a file 34
Read & Display of a File 35
Page |4

Program to show the use of classes:-


#include <iostream>

using namespace std;

class demo
{
int rn;
char name[15];
public:
void input()
{
cout<<"Enter name of student:";
cin>>name;
cout<<"Enter roll number of student:";
cin>>rn;
}
void display()
{
cout<<endl<<"Name of student="<<name<<endl;
cout<<"Roll number of student="<<rn<<endl;
}
};

int main()
{
demo obj;
obj.input();
Page |5

obj.display();
return 0;
}

Output:-
Page |6

Program to show the use of Default


Argument:-
#include <iostream>
using namespace std;
void swap(int a,int b=6);
int main()
{
int a;
cout<<"Enter the first value:"<<endl;
cin>>a;
swap(a);
return 0;
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
cout<<"After swapping values are:"<<a<<"\t"<<b<<endl;
}

Output:-
Page |7

Program of Function Overloading:-


#include <iostream>

using namespace std;

#define PI=3.14
void area(float,float);
void area(int);
void area(float);
int main()
{
float l,b,r;
int s;
cout<<"Enter the values of length and breath of rectangle:"<<endl;
cin>>l>>b;
cout<<"Enter the value of radius of circle:"<<endl;
cin>>r;
cout<<"Enter the value of side of square:"<<endl;
cin>>s;
area(l,b);
area(r);
area(s);
return 0;
}
void area(float l,float b)
{
cout<<"The area of rectangle="<<(l+b)<<endl;
}
Page |8

void area(float r)
{
cout<<"The area of circle="<<(3.14*r*r)<<endl;
}
void area(int s)
{
cout<<"The area of square="<<(s*s)<<endl;
}

Output:-
Page |9

Program to show the use of Inline


function:-
#include <iostream>
using namespace std;
inline int add(int a,int b)
{
int c;
c=a+b;
return c;
}
inline float div(float d,float e)
{j
return d/e;
}
int main()
{
cout<<"sum of 2 and 5 = "<<add(2,5)<<endl;
cout<<"division of 8 and 4 = "<<div(8,4)<<endl;
return 0;
}

Output:-
P a g e | 10

Program to show the use of Static


data member & static function:-
#include <iostream>

using namespace std;

class a
{
static int count;
public:
void get(void)
{
count++;
}
static void put(void)
{
cout<<"Count="<<count<<endl;
}
};
int a::count=10;
int main()
{
a t1,t2;
t1.get();
P a g e | 11

t2.get();
a::put();
a t3;
t3.get();
a::put();
return 0;
}

Output:-
P a g e | 12

Program to show the use of Friend


Function:-
#include <iostream>

using namespace std;

class rect2;
class rect1
{
int l,b;
public:
void get(int x,int y)
{
l=x;
b=y;
}
friend void add(rect1 r,rect2 s);
};
class rect2
{
int l,b;
public:
void get(int p,int q)
{
l=p;
b=q;
}
friend void add(rect1 r,rect2 s);
P a g e | 13

};
void add(rect1 r,rect2 s)
{
cout<<" Sum of lengths of rectangle are: "<<r.l+s.l<<endl;
cout<<" Sum of breath of rectangle are: "<<r.b+s.b<<endl;
}
int main()
{
rect1 obj;
obj.get(5,8);
rect2 obj1;
obj1.get(4,7);
add(obj,obj1);
return 0;
}

Output:-
P a g e | 14

Program of Unary Operator


Overloading:-
#include <iostream>

using namespace std;

class oper
{
int num;
public:
void input()
{
cout<<" Enter value:";
cin>>num;
}
void operator-()
{
num=-num;
}
void display()
{
cout<<" Value = "<<num<<endl;
}
};
int main()
{
oper abc;
abc.input();
P a g e | 15

-abc;
abc.display();
return 0;
}

Output:-
P a g e | 16

Program of Binary operator


Overloading:-
#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;
}

// Operator overloading
Complex operator - (Complex c2)
{
Complex temp;
temp.real = real - c2.real;
P a g e | 17

temp.imag = imag - c2.imag;

return temp;
}

void output()
{
if(imag < 0)
cout << " Output Complex number: "<< real << imag << "i"<<endl;
else
cout << " Output Complex number: " << real << "+" << imag <<
"i"<<endl;
}
};

int main()
{
Complex c1, c2, result;

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


c1.input();

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


c2.input();
result = c1 - c2;
P a g e | 18

result.output();

return 0;
}

Output:-
P a g e | 19

Program to show the use of


Constructor in C++:-
#include <iostream>

using namespace std;

class box
{
float width,height;
public:
box()
{
width=9.8;
height=8.9;
}
box(float w,float h)
{
width=w;
height=h;
}
box(box &b)
{
width=b.width;
height=b.height;
}
void area(box b);
};
void box::area(box b)
P a g e | 20

{
float a;
a=(b.width*b.height);
cout<<"Constructor call automatically after object created "<<endl;
cout<<"area="<<a<<endl;
}
int main()
{
box b1(5.6,6.5);//p call
box b2(b1);
b2.area(b1);
return 0;
}

Output:-
P a g e | 21

Program of Virtual Function:-


#include <iostream>

using namespace std;

class base
{
public:
virtual void display()
{
cout<<endl<<" D.S."<<endl;
}
void show()
{
cout<<endl<<" Show derived"<<endl;
}
};
class derived:public base
{
public:
void display()
{
cout<<endl<<" Display derived"<<endl;
}
void show()
{
cout<<endl<<"Show derived";
}
P a g e | 22

};
int main()
{
base *ptr;
base b;
derived d;
ptr=&b;
ptr->display();
ptr->show();
ptr=&d;
ptr->display();
ptr->show();
return 0;
}

Output:-
P a g e | 23

Program to show Exceptional


Handling in C++ :-
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
int a,b;
cout << " Enter 2 numbers: ";
cin >> a >> b;
try
{
if (b != 0)
{
float div = (float)a/b;
if (div < 0)
throw 'e';
cout << " a/b = " << div;
}
else
throw b;

}
catch (int e)
{
cout << " Exception: Division by zero"<<endl;
}
P a g e | 24

catch (char st)


{
cout << " Exception: Division is less than 1"<<endl;
}
catch(...)
{
cout << " Exception: Unknown";
}
return 0;
}

Output:-
P a g e | 25

Program of using Namespace:-


#include<iostream>
using namespace std;
namespace abc
{
int value()
{
return 5;
}
}
namespace xyz
{
const double pi=3.1416;
double value()
{
return 2*pi;
}
}
int main()
{
cout<<abc::value()<<endl;
cout<<xyz::value()<<endl;
cout<<xyz::pi<<endl;
return 0;
}
P a g e | 26

Output:-
P a g e | 27

Program to show the use of


Manipulators:-
#include <iostream>

using namespace std;

int main()
{

int value;
cout<<"Enter number"<<endl;
cin>>value;
cout<<"Decimal base="<<dec<<value<<endl;
cout<<"Hexadecimal base="<<hex<<value<<endl;
cout<<"Octal base="<<oct<<value<<endl;
return 0;
}

Output:-
P a g e | 28

Program of Inheritance:-
#include <iostream>
using namespace std;

class Person
{
public:
string profession;
int age;

Person(): profession("unemployed"), age(16) { }


void display()
{
cout << "My profession is: " << profession << endl;
cout << "My age is: " << age << endl;
walk();
talk();
}
void walk() { cout << "I can walk." << endl; }
void talk() { cout << "I can talk." << endl; }
};

// MathsTeacher class is derived from base class Person.


class MathsTeacher : public Person
P a g e | 29

{
public:
void teachMaths() { cout << "I can teach Maths." << endl; }
};
// Footballer class is derived from base class Person.
class Footballer : public Person
{
public:
void playFootball() { cout << "I can play Football." << endl; }
};
int main()
{
MathsTeacher teacher;
teacher.profession = "Teacher";
teacher.age = 23;
teacher.display();
teacher.teachMaths();

Footballer footballer;
footballer.profession = "Footballer";
footballer.age = 19;
footballer.display();
footballer.playFootball();
return 0;
}
P a g e | 30

Output:-
P a g e | 31

Program to show the use of Generic


Function:-
#include <iostream>

using namespace std;

template<class x>x big(x a,x b)


{
return((a>b)?a:b);
}
int main()
{
cout<<"\n bigger of two is:"<<big(21,16)<<"\n";
cout<<"\n bigger of two is:"<<big(13.44,16.54)<<"\n";
cout<<"\n bigger of two is:"<<big('E','L')<<"\n";
return 0;
}

Output:-
P a g e | 32

Program to show the use of Generic


Class:-
#include <iostream>

using namespace std;

template<class t1,class t2>


class s
{
t1 a;
t2 b;
public:
void input()
{
cout<<"enter numbers";
cin>>a>>b;
}
void swap()
{
a=a+b;
b=a-b;
a=a-b;
cout<<a<<endl<<b;
}
void output()
{
cout<<"After swapping"<<endl;
swap();
}
P a g e | 33

};
int main()
{
s <int,int>s1;
s1.input();
s1.output();
return 0;
}

Output:-
P a g e | 34

Program to Write in a File:-


#include <iostream>
#include <fstream>
using namespace std;
int main(){

char text[200];

fstream file;
file.open ("example.txt", ios::out | ios::in );

cout << "Write text to be written on file." << endl;


cin.getline(text, sizeof(text));

file << text << endl;

file.close();
return 0;
}

Output:-
P a g e | 35

Program to Read and Display a File:-


#include <iostream>
using namespace std;

#include<fstream>
int main()
{
ifstream fin;
fin.open("c:/abc.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
cout<<endl;
fin.close();
return 0;
}

Output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy