- Click below to download Unit 1 programs in pdf file: -
Unit 1
/* 1. Write a program to calculate the area of circle, rectangle and square using function overloading. */
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
int main()
{
int s,l,b;
float r;
cout<<"Enter side of a square:";
cin>>s;
cout<<"Enter length and breadth of rectangle:";
cin>>l>>b;
cout<<"Enter radius of circle:";
cin>>r;
cout<<"Area of square is : "<<area(s);
cout<<"\nArea of rectangle is : "<<area(l,b);
cout<<"\nArea of circle is : "<<area(r);
}
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
/*2 Write a program to demonstrate the use of default arguments
in function overloading. */
#include<iostream>
using namespace std;
int sum(int a,int b,int c=150,int d=250)
{
return (a+b+c+d);
}
int main()
{
cout<<sum(100,200)<<endl;
cout<<sum(100,200,300)<<endl;
cout<<sum(100,200,300,400)<<endl;
}
/*3 Write a program to demonstrate the use of returning a
reference variable.*/
#include<iostream>
using namespace std;
int y;
int &bca4gu()
{
return y;
}
int main()
{
bca4gu() = 100;
cout << y;
}
/* 4. 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.
*/
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
class student
{
public:
int roll,sci,math,eng,c,c2,total;
char name[15];
public:
void get()
{
cout<<"Enter your roll Number : ";
cin>>roll;
cout<< "Enter your Name : ";
cin>>name;
cout<< "Enter your 5 subjects marks : "<<endl;
cout<< "Science : ";
cin>>sci;
cout<< "Mathematics : ";
cin>>math;
cout<< "English : ";
cin>>eng;
cout<< "C : ";
cin>>c;
cout<< "C++ : ";
cin>>c2;
}
void total1()
{
total=sci+math+eng+c+c2;
cout<<total;
}
void display()
{
cout<<"Your Details is : "<<endl;
cout<<"Roll Number : "<<roll<<endl;
cout<<"Student Name is : "<<name<<endl;
cout<<"Marks of 5 Subject is given below : "<<endl;
cout<<endl<< "Science : "<<sci;
cout<<endl<< "Mathematics : "<<math;
cout<<endl<< "English : "<<eng;
cout<<endl<< "C : "<<c;
cout<<endl<< "C++ : "<<c2;
cout<<endl<< "Total marks is : "; total1();
cout<<endl <<endl<<endl;
}
};
int main()
{
student xy;
student ab;
xy.get();
xy.display();
ab.get();
ab.display();
}
/* 5. 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. */
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
double power(double m,int n=2);
int main()
{
double ans,m,n;
char choice;
cout<< "Enter the value of M= :";
cin>>m;
cout<<"Do you want to consider default value ? (Y/N)";
cin>>choice;
if(choice=='y' || choice=='Y')
{
ans=power(m);
cout<<"the power of "<<m<<" is "<<ans;
}
else
{
cout<< "Enter the value of N: ";
cin>>n;
ans=power(m,n);
cout<< "the power of " <<m<< " is "<<ans;
}
}
double power(double m,int n)
{
double ans;
ans=pow(m,n);
return ans;
}
/* 6. Write a basic program which shows the use of scope
resolution operator. */
#include <iostream>
using namespace std;
char a = 'y';
int b = 15;
int main()
{
char a = 'b';
cout << "The static variable : "<< ::b;
cout << "\nThe local variable : " << a;
cout << "\nThe global variable : " << ::a;
}
/* 7. Write a C++ program to swap the value of private data
members from 2 different classes. */
#include<iostream>
using namespace std;
class Swap
{
int a,b,tmp;
public:
Swap(int a,int b)
{
this->a=a;
this->b=b;
}
friend void swap(Swap&);
};
void swap(Swap& m1)
{
cout<<"Before Swapping :"<<m1.a<<" "<<m1.b;
m1.tmp=m1.a;
m1.a=m1.b;
m1.b=m1.tmp;
cout<<endl<<"After Swapping :"<<m1.a<<" "<<m1.b;
}
int main()
{
Swap m(10,20);
swap(m);
return 0;
}
/* 8. Write a program to illustrate the use of this pointer. */
#include<iostream>
using namespace std;
class thisp
{
int a;
public:
void setdata(int a)
{
this->a=a;
}
void disp()
{
cout<<a;
}
};
int main()
{
thisp y1;
y1.setdata(10);
y1.disp();
}
/* 9. 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. */
#include<iostream>
using namespace std;
int main()
{
int m[5]={0,0,0,0,0};
int n,vote,sb=0,i;
cout<< "Enter the Number of votes : ";
cin>>n;
for(i=0;i<n;i++)
{
cout<< "Enter your vote : ";
cin>>vote;
switch(vote)
{
case 1:m[0]++;
break;
case 2:m[1]++;
break;
case 3:m[2]++;
break;
case 4:m[3]++;
break;
case 5:m[4]++;
break;
default : sb++;
}
}
cout<<endl<<"Results is given below";
for(i=0;i<5;i++)
{
cout<<endl<<"Candidate " << i+1<<" : "<<m[i];
}
cout<<endl<<"Spoiled Ballots : "<<sb;
}
/* 10. Write a program to call member functions of class in the main function using pointer to object and pointer to member function. */
#include<iostream>
using namespace std;
class test
{
public:
int a;
int b;
void setdata()
{
a=10,
b=20;
};
void display()
{
cout<<"a : "<<a<<endl<<"b : "<<b;
}
};
int main()
{
test b1;
b1.setdata();
b1.display();
b1.a=33;
cout<<endl;
b1.display();
test *p;
p=&b1;
p->b=44;
cout<<endl;
p->display();
}
0 Comments