Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
74 views

Programs of File

The document contains 12 programming problems involving classes and objects in C++. Problem 1 demonstrates finding the power of a number using functions. Problem 2 uses a structure to sum the x and y coordinates of two points. Problem 3 performs basic math operations using a switch statement. Later problems demonstrate concepts like default arguments, copy constructors, constructors and destructors, and representing distance using classes.

Uploaded by

Gagandeep Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Programs of File

The document contains 12 programming problems involving classes and objects in C++. Problem 1 demonstrates finding the power of a number using functions. Problem 2 uses a structure to sum the x and y coordinates of two points. Problem 3 performs basic math operations using a switch statement. Later problems demonstrate concepts like default arguments, copy constructors, constructors and destructors, and representing distance using classes.

Uploaded by

Gagandeep Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 38

PROG 1:-Write a program to find the power of

the number.

#include<iostream.h>
#include<conio.h>
double pow(double ,int );
void main()
{
double n,r;
clrscr();
int p;
char c;
cout<<"enter any number";
cin>>n;
do
{
cout<<"do u want to enter the
power(y/n)\n";
cin>>c;
if(c=='y')
{
cout<<"enter the power to be raised";
cin>>p;
r=pow(n,p);
}
else
{
if(c=='n')
{
p=2;
r=pow(n,p);
cout<<r;
}
else
cout<<"invalid choice\n";
}
} while(c!='y' && c!='n');
getch();

double pow(double n,int p)


{
double r=1;
int i;
if(p<0)
r=pow(n,p);
else
{
for(i=1;i<=p;i++)
r=r*n;
}
cout<<r;
return(r);
}

OUTPUT:-
enter any number4
do u want to enter the power(y/n)
y
enter the power to be raised2
16
PROG 2:-write a program using structure

#include<iostream.h>
#include<conio.h>
struct point
{
int x;
int y;
}
p[2],pt={0,0};
void main()
{
int i;
for(i=0;i<=1;i++)
{
cout<<"enter x"<<i<<"and y coordinate"<<i;
cin>>p[i].x>>p[i].y;
}
for(i=0;i<=1;i++)
{
pt.x=pt.x+p[i].x;
pt.y=pt.y+p[i].y;
}
cout<<"\nsum of x coordinate
is"<<pt.x<<"\nsum of y coordinate is"<<pt.y;
getch();
}
Output:-

enter x0and y coordinate03 4


enter x1and y coordinate16 7

sum of x coordinate is9


sum of y coordinate is11
PROG 3:- WAP TO PERFORM
addtion,subtraction,multiply and divison.

#include<iostream.h>
#include<conio.h>
void main()
{
double operand1;
double operand2;
char c,x;
do
{
cout<<"enter the firstno,operator,secondno";
cin>>operand1>>x>>operand2;
switch(x)
{
case
'+':cout<<"\nanswer="<<operand1+operand2;
break;
case '-':cout<<"\nanswer="<<operand1-
operand2;
break;
case
'*':cout<<"\nanswer="<<operand1*operand2;
break;
case
'/':cout<<"\nanswer="<<operand1/operand2;
break;
default: cout<<"\ninvalid choice";
}
do
{cout<<"\nenter choice";
cin>>c;
if(c!='y' && c!='n')
cout<<"\ninvalid choice";
}
while(c!='y' && c!='n');
}
while(c=='y');
getch();
}
PROG 4:-WRITE A PROGRAM USING STRUCTURE

#include<iostream.h>
#include<conio.h>
struct phone
{
char area[10];
char exchange[10];
char phno[10];
};
void main()
{
phone p1={"212","767","89000"};
phone p2;
cout<<"\nenter your phone no.\n";
cin>>p2.area>>p2.exchange>>p2.phno;
cout<<"\n my phoneno
is\n"<<p1.area<<p1.exchange<<p1.phno;
cout<<"\n your phoneno
is\n"<<p2.area<<p2.exchange<<p2.phno;
getch();
}
OUTPUT:-

enter your phone no.


344 321 44444

my phoneno is
21276789000
your phoneno is
34432144444
PROG 5:-WRITE A PROGRAM A HOTDOG STAND THAT
ENABLES THREE KINDS OF ITERATION.

#include<iostream.h>
#include<conio.h>
class hotdogsand
{
private:
int hotdog_one_hand;
int buns_one_hand;
public:
void display()
{
cout<<hotdog_one_hand;
cout<<buns_one_hand;
}
void soldonehand()
{
--hotdog_one_hand;
--buns_one_hand;
}
void initdata()
{
cout<<"\nenter the hotdog on hand\n";
cin>>hotdog_one_hand;
cout<<"\nenter the buns on hand\n";
cin>>buns_one_hand;
}
}; //end of class

void main()
{
hotdogsand stand1;
hotdogsand stand2;
hotdogsand stand3;
clrscr();
cout<<"\nintilizes the data of stand1\n";
stand1.initdata();
cout<<"\nintilizes the data of stand2\n";
stand2.initdata();
cout<<"\nintilizes the data of stand3\n";
stand3.initdata();
char choice='x';
while(choice!='q')
{
cout<<"\nenter the choice of stand\n";
cin>>choice;
switch(choice)
{
case 1:cout<<"\n sell the data of
stand1\n";
stand1.soldonehand();
break;
case 2:cout<<"\n sell the data of
stand2\n";
stand2.soldonehand();
break;
case 3:cout<<"\n sell the data of
stand3\n";
stand3.soldonehand();
break;
}
}
cout<<"\n\ndisplay the value of
stand1\n\n";
stand1.display();
cout<<"\n\ndisplay the value of
stand2\n\n";
stand2.display();
cout<<"\n\ndisplay the value of
stand3\n\n";
stand3.display();
getch();
}

Output:-

intilizes the data of stand1

enter the hotdog on hand


23

enter the buns on hand


54

intilizes the data of stand2

enter the hotdog on hand


76

enter the buns on hand


54

intilizes the data of stand3

enter the hotdog on hand


32
enter the buns on hand
87

enter the choice of stand


q

display the value of stand1

2354

display the value of stand2

7654

display the value of stand3

3287
PROG 6:-WRITE A PROGRAM TO READ THE DATA OF N
NO. OF THE EMPLOYEE AND COMPUTE THE NET
SALARY.
#include<iostream.h>
#include<conio.h>
class employee
{
private:
int empno;
char empname[20];
int bs;
int da;
int incometax;
int ns;
public:
void getdata()
{
cout<<"\nenter the employee number\n";
cin>>empno;
cout<<"\nenter the empname\n";
cin>>name;
cout<<"\nenter the basic salary\n";
cin>>basicsalary;
}
void display()
{
cout<<"employee number is"<<empno;
cout<<"employee name is"<<empname;
cout<<"employee net salary is"<<netsalary;
}
void calculate()
{
da=52/100*bs;
incometax=30/100*gs;
ns=gs-incometax;
}
}
void main()
{

employee x[5];
int i;
clrscr();
for(i=1;i<5;i++)
{
x[i].getdata();
}
for(i=1;i<5;i++)
{
x[i].netsalary();
}
for(i=1;i<5;i++)
{
x[i].display();
}
getch();
}

Output:-
enter the name of the employee
kanchan
enter the employee number
12
enter the basic salary of the employee
12000
enter the name of the employee
juhi
enter the employee number
14
enter the basic salary of the employee
12999
name of the employee is
kanchannumber of employee is
12net salary of employee is
0name of the employee is
juhinumber of employee is
14net salary of employee is
0

PROG 7:- WRITE A PROGRAM TO CREATE A


FANCY_TEXT OBJECT AND GAVE IT TEXT”GONE WITH
WITH WIND” THEN IT DISPLAY IT TEXT USING
THREE DIFFERENT VERSION OF BOX_DISPLAY().

#include<iostream.h>
#include<conio.h>
#include<string.h>
const int Maxlength=40;
class fancy_text
{
private:
char text[Maxlength];
public:
void set_text(char tx[])
{
strcpy(text,tx);
}
void box_display();
void box_display(char);
void box_display(char, int);
};
void fancy_text::box_display()
{
cout<<"--------------------------------------
";
cout<<endl<<text<<endl;
cout<<"--------------------------------------
";
}
void fancy_text::box_display(char ch)
{
int j;
for(j=0;j<Maxlength;j++)
cout<<ch;
cout<<endl<<text<<endl;
for(j=0;j<Maxlength;j++)
cout<<ch;
}
void fancy_text::box_display(char ch,int n)
{
int m;
for(m=0;m<n;m++)
cout<<ch;
cout<<endl<<text<<endl;
for(m=0;m<n;m++)
cout<<ch;
}

void main()
{
fancy_text ft;
clrscr();
ft.set_text("gone with wind");
ft.box_display();
ft.box_display('=');
ft.box_display('=',18);
getch();
}

OUTPUT:-
--------------------------------------
gone with wind
--------------------------------------
========================================
gone with wind
=============================================
=============

PROG 8:-WRITE A PROGRAM THAT SHOWS THE


CONCEPTS OF DEFAult argument USING FANCY_TEXT
ASHISH CLASSNAME.

#include<iostream.h>
#include<conio.h>
#include<string.h>
const int Maxlength=40;
class fancy_text
{
private:
char text[Maxlength];
public:
void set_text(char tx[])
{
strcpy(text,tx);
}
void box_display(char ch ='-', int n=18)
{
int m;
for(m=0;m<n;m++)
cout<<ch;
cout<<endl<<text<<endl;
for(m=0;m<n;m++)
cout<<ch;
}
};
void main()
{
fancy_text ft;
clrscr();
ft.set_text("gone with wind");
ft.box_display('=',18);
getch();
}

OUTPUT:-
==================
gone with wind
PROG 9:-WRITE A PROGRAM TO CREATE FUNCTION
ARGUMENTS.

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
class airtime
{
int hours;
int mins;
public:
void set()
{
char dummy;
cout<<"enter time(format 23:59)";
cin>>hours>>dummy>>mins;
}
void display()
{
cout<<hours<<":"<<setfill('0')<<setw(2)<<mins
;
}
void add(airtime a1,airtime a2)
{
mins=a1.mins+a2.mins;
hours=a1.hours+a2.hours;
if(mins>59)
{
mins=mins-60;
hours=hours+1;
}
if(hours>23)
{
hours=hours-24;
}
}
}; //end of class

void main()
{
airtime t1,t2,t3;
clrscr();
cout<<"for t1";
t1.set();
cout<<"for t2";
t2.set();
t3.add(t1,t2);
cout<<"for t3=";
t3.display();
getch();
}

OUTPUT:-

for t1enter time(format 23:59)4:87


for t2enter time(format 23:59)5:98
for t3=10:125

PROG 10:-WRITE A PROGRAM TO CREATE A copy


constructor.
#include<iostream.h>
#include<conio.h>
int a=20;
int b=a;
class omega
{
private:
int intvar;
public:
omega(int v)
{
intvar=v;
}
omega(const omega&om)
{
intvar=om.intvar;
cout<<"i am copy constructor";
}
};
void main()
{
omega om1(12);
omega om2=om1;
omega om3(om1);
getch();
}

PROG 11 :-WRITE A PROGRAM TO CREATE A CLASS


using constructor and destructor

#include<iostream.h>
#include<conio.h>
class parent
{
public:
parent()
{
cout<<"parent constructor";
}
~parent()
{
cout<<"parent destructor";
}
}
class child:public parent
{
public:
child()
{
cout<<"child constructor";
}
~child()
{
cout<<"child destructor";
}
}
void main()
{
cout<<"\nstarting";
child ch;//creating a child object
cout<<"\nterminating";
}

output:-

starting

parent constructor
child constructor
terminating
child destructor
parent destructor

PROG 12:-WRITE A PROGRAM TO CREATE A CLASS


THE MODULES DISTANCE IN FEET AND INCHES.

#include<iostream.h>
#include<conio.h>
class english
{
private:
int feet;
float inch;
public:
english()
{
feet=0;
inch=0.0;
}
english(float metres)
{
const float mt=3.280883;
float ft=mt*metres;
feet= int(ft);
inch= 12*(ft-feet);
}
english(int f, float i)
{
feet=f;
inch=i;
}
void display()
{
cout<<feet<<"'-"<<inch<<'"';
}
};
void main()
{
clrscr();
english e1;
cout<<"e1=";
e1.display();
english e2(1.2);
cout<<"\ne2=";
e2.display();
english e3(3,3.2);
cout<<"\ne3=";
e3.display();
getch();
}

Output:-
e1=0'-0"
e2=3'-11.244719"
e3=3'-3.2"

Prog 13:-write aprogram that creates two


employee object then it swap function to
exchange the data into 2 objects.

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

class employee
{
private:
char ename[20];
int esno ;
public :
void input()
{
cout<<"enter the name of the employee\n";
cin>>ename;
cout<<"enter the serial number of the
employee\n";
cin>>esno;
}
void output()
{
cout<<"employee name is"<< ename<<endl;
cout<<"serail number of employee is"
<<esno<<endl;
}
};
void swap(employee& m, employee& n)
{
employee vemp=m;
m=n;
n=vemp;
}
void main()
{
void swap(employee&, employee&);
employee e1,e2;
clrscr();
cout<<"enter the value of employee 1\n";
e1.input();
cout<<"enter the value of employee 2\n";
e2.input();
swap(e1,e2);
cout<<"value of employee first is:\n";
e1.output();
cout<<"value of employee second is :\n";
e2.output();
getch();
}
Output:-
enter the value of employee 1
enter the name of the employee
kanchan
enter the serial number of the employee
12
enter the value of employee 2
enter the name of the employee
deepika
enter the serial number of the employee
14
value of employee first is:
employee name isdeepika
serail number of employee is14
value of employee second is :
employee name iskanchan
serail number of employee is12

PROG 14:-Write a program using inheritance.

#include<iostream.h>
#include<conio.h>
class employee
{
private:
char name[20];
int number;
public:
void getdata()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the number";
cin>>number;
}
void putdata()
{
cout<<"name="<<name<<endl;
cout<<"number="<<number<<endl;
}
};
class manager:public employee
{
private:
char title[20];
int dues;
public:
void getdata()
{
employee::getdata();
cout<<"enter the title";
cin>>title;
cout<<"enter the dues";
cin>>dues;
}
void putdata()
{
employee::putdata();
cout<<"title="<<title<<endl;
cout<<"dues="<<dues<<endl;
}
};
class scientist:public employee
{
private:
char publis[20];
public:
void getdata()
{
employee::getdata();
cout<<"enter the publication";
cin>>publis;
}
void putdata()
{
employee::putdata();
cout<<"publis"<<publis<<endl;
}
};
class labour:public employee
{
};
void main()
{
clrscr();
manager m1;
scientist s1;
labour l1;
cout<<"\ninput the object m1 of the class
manager\n";
m1.getdata();
cout<<"\ninput the object s1 of the class
scientist\n";
s1.getdata();
cout<<"\ninput the object l1 of the class
labour\n";
l1.getdata();
cout<<"\noutput the object m1 of the class
manager\n";
m1.putdata();
cout<<"\noutput the object s1 of the class
scientist\n";
s1.putdata();
cout<<"\noutput the object l1 of the class
labour\n";
l1.putdata();
}

Output:-
input the object m1 of the class managerenter
the namekanchan
enter the number12
enter the titletatoo
enter the dues123454
input the object s1 of the class
scientistenter the namedeepali
enter the number13
enter the publicationgdf
input the object l1 of the class labourenter
the namejuhi ▒
enter the number15
output the object m1 of the class
managername=kanchan
number=12
title=tatoo
dues=-7618
output the object s1 of the class
scientistname=deepali
number=13
publisgdf
output the object l1 of the class
labourname=juhi
number=15

Prog 15:-write a program using static data


member.

#include<iostream.h>
#include<conio.h>
class shared
{
static int a;
int b;
public:
void set(int i,int j)
{
a=i;
b=j;
}
void show();
};
int shared::a;
void shared::show()
{
cout<<"this is static a:"<<a<<endl;
cout<<"this is static b:"<<b<<endl;
cout<<"\n";
}
void main()
{
clrscr();
shared x,y;
x.set(1,1);
x.show();
y.set(2,2);
y.show();
x.show();
getch();
}

Output:-

this is static a:1


this is static b:1
this is static a:2
this is static b:2

this is static a:2


this is static b:1

prog 16:-write a program using static membetr


function.

#include<iostream.h>
#include<conio.h>
class c1
{
static int resources;
public:
static int get_resources();
void free_resources()
{
resources=0;
}
};
int c1::resources;
int c1::get_resources()
{
if(resources)
return 0;
else
return 1;
}
void main()
{
clrscr();
c1 ob1,ob2;
if(c1::get_resources());
cout<<"ob1 has resources\n";
if(!c1::get_resources())
cout<<"ob2 denied resources\n";
ob1.free_resources();
if(ob2.get_resources())
cout<<"ob2 can now use resources";
getch();
}

Output:-

ob1 has resources


ob2 can now use resources

You might also like