0% found this document useful (0 votes)
8 views14 pages

c++ project file

Uploaded by

victoryvibes8393
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)
8 views14 pages

c++ project file

Uploaded by

victoryvibes8393
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/ 14

QUES-1- Write a program to print the number is prime or not.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,count=0;
cout<<"Enter the number\n";
cin>>n;
for(i=2;i<n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count>0)
{
cout<<"Number is not prime.\n";
}
else
{
cout<<"Number is prime.";
}
getch();
}

Output-

RUN-1
Enter the number
3
Number is prime.

RUN-2
Enter the number
8
Number is not prime.

OOPs Programming Page 1


Ques-2- Write a program using object as a function argument.

#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void setdata(int x,int y)
{
a=x;
b=y;
}
void showdata()
{
cout<<"a = "<<a<<” , ”<<"b = "<<b<<endl;
}
void sum(complex x,complex y)
{
a=x.a+y.a;
b=x.b+y.b;
cout<<"Sum of complex number is:"<<endl;
cout<<"a="<<a<<” , ”<<"b="<<b;
}
};
void main()
{
complex c1,c2,c3;
c1.set.data(2,3);
c1.showdata();
c2.setdata(4,5);
c2.showdata();
c3.sum(c1,c2);
getch();
}

OUTPUT-
a=2, b=3
a=4 , b=5
Sum of complex number is:
a=6, b=8

OOPs Programming Page 2


Ques- 3- Write a program to implement function overloading.

#include<iostream.h>
#include<conio.h>
class ABC
{
int sum;
public:
int add(int a,int b)
{
sum=a+b;
return sum;
}
int add(int a,int b,int c)
{
sum=a+b+c;
return sum;
}
};

void main()
{
clrscr();
ABC x;
cout<<"Sum of number 2 and 4 is = "<<x.add(2,4)<<endl;
cout<<"Sum of number 1,3 and 5 is = "<<x.add(1,3,5);
getch();
}

OUTPUT-

Sum of number 2 and 4 is = 6


Sum of number 1,3 and 5 is = 9

OOPs Programming Page 3


Ques-4- Write a program for overloading binary ‘–‘ operator.

#include<iostream.h>
#include<conio.h>
class integer
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"the value of x= "<<x<<endl;
cout<<"the value of y= "<<y<<endl;
}

integer operator -(integer i) //binary operator


{
integer temp;
temp.x=x-i.x;
temp.y=y-i.y;
return(temp);
}
};
void main()
{
clrscr();
integer i1,i2,i3;
i1.setdata(9,7);
i2.setdata(3,4);
i1.showdata();
i2.showdata();
cout<<"After subtract : "<<endl;
i3=i1-i2; //binary operator
i3.showdata();
getch();
}

OUTPUT-

the value of x= 9
the value of y= 7
the value of x= 3
the value of y= 4
After subtract :
the value of x= 6
the value of y= 3

OOPs Programming Page 4


Ques- 5- Write a program for overloading binary ‘+’ operator.

#include<iostream.h>
#include<conio.h>
class integer
{
int x,y;
public:
void setdata(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<"the value of x= "<<x<<endl;
cout<<"the value of y= "<<y<<endl;
}
integer operator +(integer i) //binary operator
{
integer temp;
temp.x=x+i.x;
temp.y=y+i.y;
return(temp);
}
};
void main()
{
clrscr();
integer i1,i2,i3;
i1.setdata(9,7);
i2.setdata(3,4);
i1.showdata();
i2.showdata();
cout<<"After addition : "<<endl;
i3=i1+i2; //binary operator
i3.showdata();
getch();
}

OUTPUT-

the value of x= 9
the value of y= 7
the value of x= 3
the value of y= 4
After addition :
the value of x= 12
the value of y= 11

OOPs Programming Page 5


Ques-6- Write a program for multiple constructor in a class.

#include<iostream.h>
#include<conio.h>
class A
{
int a;
public:
A() // default constructor
{
a=0;
cout<<"a = "<<a<<endl;
}
A(int b) //parameterized constructor
{
a=b;
cout<<"a = "<<a<<endl;
}

A(A &b) //copy constructor


{
a=b.a;
cout<<"a = "<<a<<endl;
}
};

void main()
{
clrscr();
A x;
A y(2);
A z(y);
getch();
}

OUTPUT-

a=0
a=2
a=2

OOPs Programming Page 6


Ques- 7- Write a program for implememting multiple inheritance.

#include<iostream.h>
#include<conio.h>

class student
{
protected:
int roll;
public:
void get_roll(int a)
{
roll=a;
}
void show_roll()
{
cout<<"Roll no = "<<roll<<endl;
}
};

class test
{
protected:
int sub1,sub2;
public:
void get_marks(int x,int y)
{
sub1=x;
sub2=y;
}

void show_marks()
{
cout<<"Sub1="<<sub1<<endl;
cout<<"Sub2="<<sub2<<endl;
}
};

class result:public test,public student


{
int total;
public:
void show_result()
{
total=sub1+sub2;
cout<<"Total="<<total;
}
};

void main()
{
clrscr();
result student1;

OOPs Programming Page 7


student1.get_roll(1);
student1.show_roll();
student1.get_marks(90,94);
student1.show_marks();
student1.show_result();
getch();
}

OUTPUT-

Roll no = 1
Sub1=90
Sub2=94
Total=184

OOPs Programming Page 8


Ques- 8- Write a program using virtual function.

#include<iostream.h>
#include<conio.h>
class A
{
protected :
int a1,b1;
public:
virtual void setdata(int x1,int y1) //virtual function
{
a1=x1;
b1=y1;
}
virtual void showdata() //virtual function
{
cout<<"a1="<<a1<<endl<<"b1="<<b1<<endl;
}
};
class B:public A
{
protected :
int a2,b2;
public:
void setdata(int x2,int y2)
{
a2=x2;
b2=y2;
}
void showdata()
{
cout<<"a2="<<a2<<endl<<"b2="<<b2<<endl;
}
};
void main()
{
A *p,obj1;
p=&obj1;
(*p).setdata(3,6);
(*p).showdata();
B obj2;
p=&obj2;
(*p).setdata(13,16);
(*p).showdata();
getch();
}
OUTPUT-
a1=3
b1=6
a2=13
b2=16

OOPs Programming Page 9


Ques- 9- Write a program using destructor in a class.

#include<iostream.h>
#include<conio.h>
class book
{
int id;
int price;
int page;
public:
book(int i,int pr,int p)
{
id=i;
price=pr;
page=p;
}

~book()
{
cout<<"book id = "<<id<<endl
<<"book price = "<<price<<endl
<<"book pages = "<<page<<endl;
}

};

void main()
{
clrscr();
{
book b(1001,285,4560);
}
getch();
}

OUTPUT-

book id = 1001
book price = 285
book pages = 4560

OOPs Programming Page 10


Ques -10- Write a programfor swapping two numbers using call by reference method.

#include<iostream.h>
#include<conio.h>

class swapping
{
int a,b,temp;
public:
void swap(int &x,int &y)
{
temp=x;
x=y;
y=temp;
}
};
void main()
{
clrscr();
int a=5,b=7;
swapping b1;
cout<<"Before swapping:\n"
<<"a="<<a<<endl
<<"b="<<b<<endl;
b1.swap(a,b);

cout<<"After swapping:\n"
<<"a="<<a<<endl
<<"b="<<b<<endl;

getch();
}

OUTPUT-
Before swapping:
a=5
b=7

After swapping:
a=7
b=5

OOPs Programming Page 11


Ques- 11- Write a program to number is armstrong or not.

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int m,n,d,r,sum=0;
cout<<"Enter the number\n";
cin>>n;
m=n;
while(n>0)
{
d=n/10;
r=n%10;
sum=sum+(r*r*r);
n=d;
}
if(m==sum)
{
cout<<"Number is armstrong\n";
}
else
{
cout<<"Number is not armstrong";
}
getch();
}

OUTPUT-

RUN-1

Enter the number


23
Number is not armstrong.

RUN-2

Enter the number


153
Number is armstrong.

OOPs Programming Page 12


Ques- 12- Write a program to implememt multilevel inheritance.

#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll;
public:
void get_roll(int a)
{
roll=a;
}
void show_roll()
{
cout<<"Roll no = "<<roll<<endl;
}
};
class test:public student
{
protected:
int sub1,sub2;
public:
void get_marks(int x,int y)
{
sub1=x;
sub2=y;
}
void show_marks()
{
cout<<"Sub1="<<sub1<<endl;
cout<<"Sub2="<<sub2<<endl;
}
};
class result:public test
{
int total;
public:
void show_result()
{
total=sub1+sub2;
cout<<"Total="<<total;
}
};
void main()
{
clrscr();
result student1;
student1.get_roll(1);
student1.show_roll();
student1.get_marks(92,97);
student1.show_marks();
student1.show_result();
getch();
}

OOPs Programming Page 13


OUTPUT-

Roll no = 1
Sub1=92
Sub2=97
Total=189

OOPs Programming Page 14

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