C++ Pract Ycm
C++ Pract Ycm
#include <iostream>
int main()
int x;
int y;
float z;
x=10;
y=15;
z=12.6;
cout<<"\n";
system("PAUSE");
return EXIT_SUCCESS;
}
2.Write a c++ program to prompt to prompt the user to input
her/his name and print this name on the screen,as shown
below.The text from keyboard can be read by using cin>>and
to display the text on the screen you can use cout<<.
#include <cstdlib>
#include <iostream>
int main()
char name[20];
cin>>name;
system("PAUSE");
return EXIT_SUCCESS;
}
3.Write a c++ program that prompts the user to input
three integer values and find the greatest value of three
values.
#include <cstdlib>
#include <iostream>
#include<iomanip>
int main()
int a,b,c,max;
cin>>a>>b>>c;
system("PAUSE");
return EXIT_SUCCESS;
}
4.Write a program that determines a students grade .The
program will read three types of scores (quiz,mid-term,and
final scores)and determine the grade based on the following
rules:
if the average score =90%=>grade=A
if the average score >=70%and <90%=>grade=B
if the average score> =50%and <70%=>grade=C
if the average score <50%=>grade=F
#include <cstdlib>
#include <iostream>
#include<iomanip>
int main
float quiz_score;
float mid_score;
float final_score;
float avg;
cin>>mid_score = float.Parse;
cout<<"Enter final score:";
cin>>final_score = float.Parse;
avg = (quiz_score +mid_score+final_score) / 3;
cout<<"Grade A";
cout<<"Grade B";
cout<<"Grade C";
cout<<"Grade F";
else
cout<<"Invalid input";
system("PAUSE");
return EXIT_SUCCESS;
}
}
5.Define a class called as circle which has radius as its data
member .The class should have following member functions
a.function to set the value of radius
b.function to get the value of radius
c.function to calculate and return the area of circle
d.function to calculate and return circumference
#include<iostream>
class circle
public:
circle()
cin>>radius;
};
int main()
return 0;
}
6.Develop a class to represent one digit counter .The class
must have data member to represent counter .The class
should have following function
a.function to set the value of the counter
b.function to display value of the counter
c.function to increment the counter
d.function to decrement the counter
#include<iostream.h>
#include<conio.h>
class sample
int counter;
int a;
public:
void set()
cin>>a;
counter=a;
}
void display()
cout<<"\nCounter="<<counter;
void increment()
counter++;
void decrement()
counter--;
};
void main()
clrscr();
sample s;
s.set();
s.display();
cout<<"\nAfter Increment";
s.increment();
s.display();
cout<<"\nAfter Decrement";
s.decrement();
s.display();
getch();
}
7.Define a class called as distance represented in feet and
inches.The class should have following member function
a.function to set the distance
b.function to get the distance from user
c.function to display the distance from user
d.function to add two distances and return the addition
#include <iostream>
using namespace std;
class Distance
{
private:
int feet;
int inches;
public:
void set_distance()
{
cout<<"Enter feet: ";
cin>>feet;
cout<<"Enter inches: ";
cin>>inches;
}
void get_distance()
{
cout<<"Distance is feet= "<<feet<<", inches=
"<<inches<<endl;
}
void add(Distance d1, Distance d2)
{
feet = d1.feet + d2.feet;
inches = d1.inches + d2.inches;
feet = feet + (inches / 12);
inches = inches % 12;
}
};
int main()
{
Distance d1, d2, d3;
d1.set_distance();
d2.set_distance();
d3.add(d1, d2);
d3.get_distance();
return 0;
}
8.Define a class period which has hours and miutes as its data
mamber .function add to add the periods and return the
addition .The function should work as friend function.
#include <iostream>
class A
int value;
public:
A()
value = 5;
};
class B
int value;
public:
B()
value = 3;
};
int main()
A a;
B b;
return 0;
}
9. a) create a class to demonstrate use of constructor
b)Write a program to demonstrate use of copy
constructor.
a)
#include <iostream>
class construct {
public:
int a, b;
// Default Constructor
construct()
a = 10;
b = 20;
};
int main()
{
construct c;
return 1;
b)
#include "iostream"
class point {
private:
double x, y;
public:
x = px, y = py;
};
int main(void) {
point a[10];
}
10.Define a class that has following data mamber functions
a. i=Inc,dec,display
c.Destructor function
a.
#include <stdio.h>
int main()
int i=1;
while(i<10)
printf("%d ",i);
i++;
#include <stdio.h>
int main()
{
int i=20;
while(i>10)
printf("%d ",i);
i--;
b.
#include <iostream>
class construct {
public:
int a, b;
// Default Constructor
construct()
a = 10;
b = 20;
}
};
int main()
construct c;
return 1;
c.
#include <iostream>
class HelloWorld{
public:
//Constructor
HelloWorld(){
cout<<"Constructor is called"<<endl;
}
//Destructor
~HelloWorld(){
cout<<"Destructor is called"<<endl;
//Member function
void display(){
cout<<"Hello World!"<<endl;
};
int main(){
//Object created
HelloWorld obj;
obj.display();
return 0;
#include<conio.h>
class complex {
int a, b, c;
public:
complex() {
void getvalue() {
void operator++() {
a = ++a;
b = ++b;
void operator--() {
a = --a;
b = --b;
void display() {
};
void main() {
clrscr();
complex obj;
obj.getvalue();
obj++;
obj.display();
obj--;
obj.display();
getch();
}
12. Design a class for multilevel inheritance using public and private
derivation.
#include <iostream>
public:
int x;
void getdata()
};
public:
int y;
void readdata()
};
class derive2 : public derive1 // derived from class derive1
private:
int z;
public:
void indata()
void product()
};
int main()
a.getdata();
a.readdata();
a.indata();
a.product();
return 0; }
13.Write a program to demonstrate the concept of method overriding
,virtual function
#include <iostream>
class base {
public:
void show()
};
public:
void print()
{
void show()
};
int main()
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
}
14.Desing a class FileDemo,open the file in read mode and
display the total number of line ,word characters
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
void main()
clrscr();
ifstream ifile;
cout<<"Enter file name to read and display its content (like file.txt) : ";
cin>>fname;
ifile.open(fname);
if(!ifile)
getch();
exit(0);
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
cout<<"\n";
ifile.close();
getch();
}
15.Show the implementation of template class library for
swap function
#include<iostream.h>
#include<conio.h>
template<class T>
T temp;
temp=a;
a=b;
b=temp;
return(0);
void main()
int ix,iy;
float fx,fy;
clrscr();
cout<<"\nBefore swap";
cout<<"\na="<<ix;
cout<<"\nb="<<iy;
swap(ix,iy);
cout<<"\nAfter swap";
cout<<"\na="<<ix;
cout<<"\nb="<<iy;
cin>>fx>>fy;
cout<<"\nBefore swap";
cout<<"\na="<<fx;
cout<<"\nb="<<fy;
swap(fx,fy);
cout<<"\nAfter swap";
cout<<"\na="<<fx;
cout<<"\nb="<<fy;
getch();