0% found this document useful (0 votes)
26 views32 pages

C++ Pract Ycm

The document contains multiple C++ programming examples covering various topics such as variable declaration, user input, finding maximum values, calculating grades, class definitions, operator overloading, inheritance, virtual functions, file handling, and template functions. Each example demonstrates specific programming concepts and includes code snippets for implementation. The examples range from simple variable assignments to more complex class structures and functionalities.

Uploaded by

laita nikam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views32 pages

C++ Pract Ycm

The document contains multiple C++ programming examples covering various topics such as variable declaration, user input, finding maximum values, calculating grades, class definitions, operator overloading, inheritance, virtual functions, file handling, and template functions. Each example demonstrates specific programming concepts and includes code snippets for implementation. The examples range from simple variable assignments to more complex class structures and functionalities.

Uploaded by

laita nikam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

1.

Write a c++program to declare two integer ,one float variables and


assing 10,15,and 12.6 them respectively and them prints these values
on the screen
#include <cstdlib>

#include <iostream>

using namespace std;

int main()

int x;

int y;

float z;

x=10;

y=15;

z=12.6;

cout<<"x="<<x <<"\t"<<"y= "<<y<<"\t"<<"z="<<z;

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>

using namespace std;

int main()

char name[20];

cout<<"Enter your name:";

cin>>name;

cout<<"Hello "<<name<<"! \n";

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>

using namespace std;

int main()

int a,b,c,max;

cin>>a>>b>>c;

max=a; //let max take the first value

if(max<b) max=b; // compare max with b and update max

if(max<c) max=c; //compare max with c and take c

cout<<"Max: "<<max; //output max

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>

using namespace std;

int main

float quiz_score;

float mid_score;

float final_score;

float avg;

cout<<"Enter quiz score:";


cin>>quiz_score=float.Parse;
cout<<"Enter mid-term score:";

cin>>mid_score = float.Parse;
cout<<"Enter final score:";

cin>>final_score = float.Parse;
avg = (quiz_score +mid_score+final_score) / 3;

if (avg >= 90)

cout<<"Grade A";

else if ((avg >= 70) && (avg < 90))

cout<<"Grade B";

else if ((avg >= 50) && (avg < 70))

cout<<"Grade C";

else if (avg < 50)

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>

using namespace std;

class circle

float radius, area; //data members

public:

circle()

cout<<"\n Enter the value of Radius : ";

cin>>radius;

void calculate(); //member function


void display(); //member function

};

inline void circle :: calculate() //accessing data members of a class


circle

area = 3.14 * radius * radius;

inline void circle :: display()

cout<<"\n Area of Circle : "<<area;

int main()

circle cr; //object created

cr.calculate(); //calling function

cr.display(); //calling function

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()

cout<<"Enter any no\n";

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>

using namespace std;

class B; //declaration of class B

class A

int value;

public:

A()

value = 5;

friend int sum(A, B); // declaring friend function

};

class B

int value;
public:

B()

value = 3;

friend int sum(A, B); // declaring friend function

};

int sum( A v1, B v2 ) // friend function definition

return (v1.value + v2.value);

int main()

A a;

B b;

cout << "Sum : " << sum( a, b ) << endl;

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>

using namespace std;

class construct {

public:

int a, b;

// Default Constructor

construct()

a = 10;

b = 20;

};

int main()
{

// Default constructor called automatically

// when the object is created

construct c;

cout << "a: " << c.a << endl

<< "b: " << c.b;

return 1;

b)

#include "iostream"

using namespace std;

class point {

private:

double x, y;

public:

// Non-default Constructor & default Constructor

point (double px, double py) {

x = px, y = py;

};
int main(void) {

// Define an array of size 10 & of type point

// This line will cause error

point a[10];

// Remove above line and program will compile without error

point b = point(5, 6);

}
10.Define a class that has following data mamber functions

a. i=Inc,dec,display

b.Constructor with default parameter zero

c.Destructor function

a.

//Example for increment operators

#include <stdio.h>

int main()

int i=1;

while(i<10)

printf("%d ",i);

i++;

//Example for decrement operators

#include <stdio.h>

int main()
{

int i=20;

while(i>10)

printf("%d ",i);

i--;

b.
#include <iostream>

using namespace std;

class construct {

public:

int a, b;

// Default Constructor

construct()

a = 10;

b = 20;
}

};

int main()

// Default constructor called automatically

// when the object is created

construct c;

cout << "a: " << c.a << endl

<< "b: " << c.b;

return 1;

c.
#include <iostream>

using namespace std;

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;

//Member function called

obj.display();

return 0;

*Define a class to overload unary ++ and unary -- oparator


#include<iostream.h>

#include<conio.h>

class complex {
int a, b, c;

public:

complex() {

void getvalue() {

cout << "Enter the Two Numbers:";

cin >> a>>b;

void operator++() {

a = ++a;

b = ++b;

void operator--() {

a = --a;

b = --b;

void display() {

cout << a << "+\t" << b << "i" << endl;

};

void main() {
clrscr();

complex obj;

obj.getvalue();

obj++;

cout << "Increment Complex Number\n";

obj.display();

obj--;

cout << "Decrement Complex Number\n";

obj.display();

getch();

}
12. Design a class for multilevel inheritance using public and private
derivation.
#include <iostream>

using namespace std;

class base //single base class

public:

int x;

void getdata()

cout << "Enter value of x= "; cin >> x;

};

class derive1 : public base // derived class from base class

public:

int y;

void readdata()

cout << "\nEnter value of y= "; cin >> y;

};
class derive2 : public derive1 // derived from class derive1

private:

int z;

public:

void indata()

cout << "\nEnter value of z= "; cin >> z;

void product()

cout << "\nProduct= " << x * y * z;

};

int main()

derive2 a; //object of derived class

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>

using namespace std;

class base {

public:

virtual void print()

cout << "print base class" << endl;

void show()

cout << "show base class" << endl;

};

class derived : public base {

public:

void print()
{

cout << "print derived class" << endl;

void show()

cout << "show derived class" << endl;

};

int main()

base* bptr;

derived d;

bptr = &d;

// virtual function, binded at runtime

bptr->print();

// Non-virtual function, binded at compile time

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;

char s[100], fname[20];

cout<<"Enter file name to read and display its content (like file.txt) : ";

cin>>fname;

ifile.open(fname);

if(!ifile)

cout<<"Error in opening file..!!";

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 swap(T &a,T &b)

T temp;

temp=a;

a=b;

b=temp;

return(0);

int swap(int &x,int &y);

float swap(float &p,float &q);

void main()

int ix,iy;

float fx,fy;

clrscr();

cout<<"Enter two integers\n";


cin>>ix>>iy;

cout<<"\nBefore swap";

cout<<"\na="<<ix;

cout<<"\nb="<<iy;

swap(ix,iy);

cout<<"\nAfter swap";

cout<<"\na="<<ix;

cout<<"\nb="<<iy;

cout<<"\nEnter two floating point numbers\n";

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();

You might also like