Gujarat University BCA Sem 3 C++ Practical Unit 1 | GU BCA Sem 3 OOP Practical
Q1. Write a C++ program to calculate the area of a circle, rectangle, and square using function overloading. ( GU Sem 3 OOP Practicals )
void area(int);
void area(int,int);
void areaa(int);
void main()
{
int r,l,b,s;
clrscr();
cout<<"Enter Radius of Cirle:"; cin>>r;
area(r);
cout<<"nnEnter Length of Rectangle:"; cin>>l;
cout<<"Enter Breadth of Rectangle:"; cin>>b;
area(l,b);
cout<<"nnEnter side of Square:"; cin>>s;
areaa(s);
getch();
}
void area(int a)
{
cout<<"Area of Circle is:" <<3.14aa;
}
void area(int a,int b)
{
cout<<"Area of Rectangle is:"<<a*b;
}
void areaa(int a)
{
cout<<"Area of Square is:"<<a*a;
}
Q2. Write a C++ program to demonstrate the use of default arguments in function overloading. ( GU Sem 3 C++ Practicals ).
class stu
{
int id,age;
char name[20];
public:
void info(int a)
{
id=a;
strcpy(name,"rk");
age=12;
}
void info()
{
cout<<"Id:- t"<<id<<endl;
cout<<"Name:- t"<<name<<endl;
cout<<"Age:- t"<<age;
}
};
void main()
{
clrscr();
stu x;
x.info(1);
x.info();
getch();
}
Q3. Write a C++ program to demonstrate the use of returning a reference variable. ( GU Sem 3 C++ Practicals ).
int &max(int &a,int &b,int &c)
{
if(a>b && a>c)
return a;
else if(b>c)
return b;
else
return c;
}
void main()
{
int a,b,c;
clrscr();
cout<<"Enter No."; cin>>a;
cout<<"Enter No."; cin>>b;
cout<<"Enter No."; cin>>c;
cout<<"Maximum Number:"<<max(a,b,c);
getch();
}
Q4. Create a class student which stores the detail about roll no, name, marks of 5 subjects, i.e. science, Mathematics, English, C, C++. The class must have the following:
• Get function to accept value of the data members.
• Display function to display values of data members.
• Total function to add marks of all 5 subjects and store it in the data members named total (GU Sem 3 OOP Practicals)
class student
{
int roll;
char name[10];
float mark[5],sum;
public:
void get(void);
void total(void);
void disp(void);
};
void student::get(void)
{
int i;
cout<<"NAME :"; cin>>name;
cout<<"ROLL NO.:"; cin>>roll;
for(i=1;i<=5;i++) { cout<>mark[i];
}
}
void student::disp(void)
{
int i;
cout<<"nSTUDENT NAME :"<<name;
cout<<"nSTUDENT ROLL NO:"<<roll;
for(i=1;i<=5;i++)
{
cout<<"n"<<i<<".SUBJECT MARKS:"<<mark[i];
}
}
void student::total(void)
{
int i,sum=0;
for(i=1;i<=5;i++)
{
sum=sum+mark[i];
}
cout<<"nTOTAL MARKS :"<<sum;
}
void main()
{
clrscr();
student s1;
s1.get();
s1.disp();
s1.total();
getch();
}
GU BCA Sem 3 OOP Practical Unit 1
Q5. Create a function power() to raise a number m to power n the function takes a double value for m and int value for n, and returns the result correctly. Use the default value of 2 for n to make the function calculate squares when this argument is omitted. Write a main that gets the values of m and n from the user to test the function. (GU Sem 3 C++ Practicals)
class square
{
int n;
double m;
public:
void power(double m, int n)
{
double p;
p=pow(m,n);
result(p);
}
void result(double p)
{
cout<<"Result :- "<<p;
}
};
void main()
{
clrscr();
square s;
int n;
double m;
cout<<"Enter Number :- "; cin>>m;
cout<<"Enter Power :- "; cin>>n;
s.power(m,n);
getch();
}
GU BCA Sem 3 C++ Practical Unit 1
Q6. Write a C++ basic program which shows the use of scope resolution operator. (GU Sem 3 OOP Practicals)
int a=10;
void main()
{
clrscr();
int a=20;
int b;
b=a;
// inner block
{
int a=30;
int b=12;
// most inner block
{
int a=40;
cout<<endl<<"Most Inner Block "<<endl;
cout<<" A t:- "<<a;
cout<<endl<<" A:: t:- "<<::a<<endl;
}
cout<<endl<<"Inner Block "<<endl;
cout<<" A t:- "<<a<<endl;
cout<<" A:: t:- "<<::a<<endl;
cout<<" B t:- "<<b<<endl;
}
cout<<endl<<"In Main Function "<<endl;
cout<<" A t:- "<<a<<endl;
cout<<" A:: t:- "<<::a<<endl;
cout<<" B t:- "<<b;
getch();
}
GU BCA Sem 3 OOP Practical Unit 1
Q7. Write a C++ program to swap the value of private data members from 2 different classes. (GU Sem 3 C++ Practicals)
class num2;
class num1
{
int n1;
public:
void getn1(int x)
{
n1=x;
}
friend void swap(num1,num2);
};
class num2
{
int n2;
public:
void getn2(int y)
{
n2=y;
}
friend void swap(num1,num2);
};
void swap(num1 p,num2 q)
{
p.n1=p.n1-q.n2;
q.n2=p.n1+q.n2;
p.n1=q.n2-p.n1;
cout<<"Num1 = "<<p.n1<<endl<<"Num2 = "<<q.n2;
}
void main ()
{
clrscr();
num1 x;
num2 y;
int a,b;
cout<<"Enter Two Numbers:- "; cin>>a>>b;
x.getn1(a);
y.getn2(b);
swap(x,y);
getch();
}
GU BCA Sem 3 C++ Practical Unit 1
Q8. Write a C++ program to illustrate the use of this pointer. (GU Sem 3 OOP Practicals)
class t
{
int a;
public:
void setdata(int a)
{
this->a=a;
}
void disp()
{
cout<<a;
}
};
void main()
{
t t1;
clrscr();
t1.setdata(10);
t1.disp();
getch();
}
GU BCA Sem 3 C++ Practical Unit 1
Q9. An election is contested by five candidates. The candidates are numbered 1 to 5 and the voting is done by marking the candidate number on the ballot paper. Write a program to read the ballots and count the votes cast for each candidate using an array variable count. In case a number is read outside the range of 1 to 5, the ballot should be considered as a ‘spoilt ballot’ and the program should also count the number of spoilt ballots. (GU Sem 3 C++ Practicals)
class el
{
int arry[5],spoilt;
int ch;
public:
void set();
void count(int);
void disp(int);
};
void el::set()
{
int i;
for(i=0;i<5;i++)
{
arry[i]=0;
spoilt=0;
}
}
void el::count(int n)
{
switch(n)
{
case 1:
arry[1]++;
break;
case 2:
arry[2]++;
break;
case 3:
arry[3]++;
break;
case 4:
arry[4]++;
break;
case 5:
arry[5]++;
break;
default:
spoilt++;
break;
}
}
void el::disp(int n)
{
if(n<=5)
cout<<arry[n];
else
cout<<spoilt;
}
void main()
{
int ch;
char a='y';
el ob1;
clrscr();
cout<<"S E L E C T C H O I C E"; cout<<"n1.BJP"; cout<<"n2.CON"; cout<<"n3.NBS"; cout<<"n4.DOD"; cout<<"n5.SBS"; ob1.set(); do{ cout<<"nEnter Number:"; cin>>ch;
ob1.count(ch);
ob1.disp(ch);
cout<<"nY=VOTES||N=EXITE:"; cin>>a;
}
while(a=='y' || a=='Y');
getch();
}
GU BCA Sem 3 OOP Practical
Post a Comment
Please do not enter any spam link in the comment box.