Gujarat University BCA Sem 3 C++ Practical Unit 1 | BCA Sem 3 OOP Practical


Here in this post you will find all the C++ practicals of BCA sem 3 unit 1. Gujarat University BCA Sem 3 C++ Practical Unit 1, GU BCA Sem 3 C++ Practical. 


C++ Unit 1 : Classes And Objects

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 circle,rectangle and square using function overloading. ( GU Sem 3 OOP Practicals )


Ans : 
                  
#include<iostream.h>
#include<conio.h>
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<<"\n\nEnter Length of Rectangle:";
cin>>l;
cout<<"Enter Breadth of Rectangle:";
cin>>b;
area(l,b);

cout<<"\n\nEnter side of Square:";
cin>>s;
areaa(s);
getch();
}

void area(int a)
{
cout<<"Area of Circle is:" <<3.14*a*a;
}

void area(int a,int b)
{
cout<<"Area of Rectangle is:"<<a*b;
}

void areaa(int a)
{
cout<<"Area of Square is:"<<a*a;
}


GU BCA Sem 3 OOP Practical Unit 1


Q2. Write a C++ program to demonstrate the use of default arguments in function overloading. ( GU Sem 3 C++ Practicals ).


#include<iostream.h>
#include<conio.h>
#include<string.h>
class stu
{
int id,age;
char name[20];
public:
void info(int a)
{
id=a;
strcpy(name,"Mukesh");
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();
}

GU BCA Sem 3 OOP Practical Unit 1


Q3. Write a C++ program to demonstrate the use of returning a reference variable. ( GU Sem 3 C++ Practicals ).

#include<iostream.h>
#include<conio.h>
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();
}

GU BCA Sem 3 C++ Practical Unit 1


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)

#include<iostream.h>
#include<conio.h>
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<<i<<".Subject Marks:";
cin>>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)



#include<iostream.h>
#include<conio.h>
#include<math.h>
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)

#include<iostream.h>
#include<conio.h>
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)


#include<iostream.h>
#include<conio.h>
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)

#include<iostream.h>
#include<conio.h>
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)


#include<iostream.h>
#include<conio.h>
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



Q10. Write a C++ program to call member function of class in the main function using pointer to object and to member function.  (GU Sem 3 OOP Practicals)

#include<iostream.h>
#include<conio.h>
class a{
int a;
public:
int b;
void setdata()
{
a=4,
b=9;
};

void display()
{
cout<<"A:"<<a<<endl<<"B:"<<b;
}
};

void main()
{
a obj;
clrscr();
obj.setdata();
obj.display();

obj.b=8;
cout<<endl;
obj.display();

a *p;
p=&obj;
p->b=7;
cout<<endl;
p->display();
getch();
}


Unit 3 OOP Practical Answer : Click Here

 

NOTE : OOP UNIT 2 AND 4 UPLOAD SOON 


Comment Your Views :) 

1 Comments

  1. please upload unit 2 practicals immediatly i needs it

    ReplyDelete

Post a Comment

Previous Post Next Post