Articles by "Class"
Showing posts with label Class. Show all posts
no image
Write a C++ program to convert lowercase to uppercase using files.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Read the file name.
Step 4: open the file to write the contents.
Step 5: writing the file contents up to reach a particular condition.
Step 6: write the file contents as uppercase.
Step 7: open the file to read the contents.


Program Code in C++:
#include<fstream.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
void main()
{
char c,u;
char fname[10];
clrscr();
ofstream out;
cout<<"Enter File Name:";
cin>>fname;
out.open(fname);
cout<<"Enter the text(Enter # at end)\n"; //write contents to file
while((c=getchar())!='#')
{
u=c-32;
out<<u;
}
out.close();
ifstream in(fname); //read the contents of file
cout<<"\n\n\t\tThe File contains\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}





Program Output:
Enter File Name: welcome.txt
Enter contents to store in file (enter # at end)
oops programming
The File Contains
OOPS PROGRAMMING
no image
Write a C++ program to perform the write operation within a file.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Read the file name.
Step 4: open the file to write the contents.
Step 5: writing the file contents up to reach a particular condition.

Program Code in C++:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
ofstream out;
cout<<"Enter File name:";
cin>>fname;
out.open(fname);
cout<<"Enter contents to store in file (Enter # at end):\n";
while((c=getchar())!='#')
{
out<<c;
}
out.close();
getch();
}
}





Program Output:
Enter File name: hello.txt
Enter contents to store in file (enter # at end)
Master of Computer Applications#
no image
Write a C++ program to read the content of a file.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the variables.
Step 3: Get the file name to read.
Step 4: Using ifstreamin(filename) check whether the file exist.
Step 5: If the file exist then check for the end of file condition.
Step 6: Read the contents of the file.
Step 7: Print the contents of the file.
Step 8: Stop the program.


Program Code in C++:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
clrscr();
cout<<"Enter file name:";
cin>>fname;
ifstream in(fname);
if(!in)
{
cout<<"File Does not Exist";
getch();
return;
}
cout<<"\n\n";
while(in.eof()==0)
{
in.get(c);
cout<<c;
}
getch();
}





Program Output:
Enter File name: hello.txt
Master of Computer Applications
no image
Write a C++ program to illustrate the concept of standard template library.




Program Algorithm:
Step 1: Include the header files
Step 2: Create a vector to store int
Step 3: Display the original size to store int
Step 4: Push 5 values into the vector using for loop
Step 5: Display extended size of vec
Step 6: Access 5 values from the vector
Step 7: Use iterator to access the values


Program Code in C++:
#include <iostream.h>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
int i;
cout << "vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++)
{
vec.push_back(i);
}
cout << "extended vector size = " << vec.size() << endl;
for(i = 0; i < 5; i++)
{
cout << "value of vec [" << i << "] = " << vec[i] << endl;
}
vector<int>::iterator v = vec.begin();
while( v != vec.end()) 
{
cout << "value of v = " << *v << endl;
v++;
}
return 0;
}





Program Output:
vector size = 0
extended vector size = 5
value of vec [0] = 0
value of vec [1] = 1
value of vec [2] = 2
value of vec [3] = 3
value of vec [4] = 4
value of v = 0
value of v = 1
value of v = 2
value of v = 3
value of v = 4
no image
Write a C++ program to perform exception handling with multiple catch.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare and define the function test().
Step 3: Within the try block check whether the value is greater than zero or not.
a. if the value greater than zero throw the value and catch the corresponding exception.
b. Otherwise throw the character and catch the corresponding exception.
Step 4: Read the integer and character values for the function test().


Program Code in C++:
#include<iostream.h>
#include<conio.h>
void test(int x)
{
try
{
if(x>0)
throw x;
else
}
throw 'x';
catch(int x)
{
cout<<"Catch a integer and that integer is:"<<x;
}
catch(char x)
{
cout<<"Catch a character and that character is:"<<x;
}
}
void main()
{
clrscr();
cout<<"Testing multiple catches\n:";
test(10);
test(0);
getch();
}





Program Output:
Testing multiple catches
Catch a integer and that integer is: 10
Catch a character and that character is: x
no image
Write a C++ program to perform exception handling for Divide by zero Exception.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the variables a,b,c.
Step 3: Read the values a,b,c,.
Step 4: Inside the try block check the condition.
a. if(a-b!=0) then calculate the value of d and display.
b. otherwise throw the exception.
Step 5: Catch the exception and display the appropriate message.

Program Code in C++:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c;
float d;
clrscr();
cout<<"Enter the value of a:";
cin>>a;
cout<<"Enter the value of b:";
cin>>b;
cout<<"Enter the value of c:";
cin>>c;
try
{
if((a-b)!=0)
{
d=c/(a-b);
cout<<"Result is:"<<d;
}
else
{
throw(a-b);
}
}
catch(int i)
{
cout<<"Answer is infinite because a-b is:"<<i;
}
getch();
}





Program Output:
Enter the value for a: 20
Enter the value for b: 20
Enter the value for c: 40
Answer is infinite because a-b is: 0
no image
Write a C++ program to swap the numbers using the concept of function template.

Program Algorithm:
Step 1: Include the header files
Step 2: Declare the template class.
Step 3: Declare and define the functions to swap the values.
Step 4: Declare and define the functions to get the values.
Step 5: Read the values and call the corresponding functions.
Step 6: Display the results.

Program Code in C++:
#include<iostream.h>
#include<conio.h>
template<class t>
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void fun(int a,int b,float c,float d)
{
cout<<"\na and b before swaping :"<<a<<"\t"<<b;
swap(a,b);
cout<<"\na and b after swaping :"<<a<<"\t"<<b;
cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
swap(c,d);
cout<<"\nc and d after swaping :"<<c<<"\t"<<d;
}
void main()
{
int a,b;
float c,d;
clrscr();
cout<<"Enter A,B values(integer):";
cin>>a>>b;
cout<<"Enter C,D values(float):";
cin>>c>>d;
fun(a,b,c,d);
getch();
}





Program Output:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50
no image
Write a C++ program to add two complex numbers using binary operator overloading.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.

Program Code in C++:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue(); result =
obj1+obj2; result1=obj1-
obj2; cout<<"Input
Values:\n"; obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}





Program Output:
Enter the value of Complex Numbers a, b
7 8
Enter the value of Complex Numbers a, b
5 2
Input Values
7 + 8i
5 + 2i

Result
12 + 10i
2 + 6i
no image
Write a C++ program to find the complex numbers using unary operator overloading.

Program Algorithm:
Step 1: Include the header files
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator ++ to increment the values
Step 6: Define the function operator - -to decrement the values.
Step 7: Define the display function.
Step 8: Declare the class object.
Step 9: Call the function getvalue
Step 10: Call the function operator ++() by incrementing the class object and call the function display.
Step 11: Call the function operator - -() by decrementing the class object and call the function display.




Program Code in C++:
#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();
}





Program Output:
Enter the two numbers: 3 6
Increment Complex Number
4 + 7i
Decrement Complex Number
3 + 6i
no image
Write a C++ program to illustrate virtual function.

Program Algorithm:
Step 1: Include the header files
Step 2: Declare the base class base.
Step 3: Declare and define the virtual function show().
Step 4: Declare and define the function display().
Step 5: Create the derived class from the base class.
Step 6: Declare and define the functions display() and show().
Step 7: Create the base class object and pointer variable.
Step 8: Call the functions display() and show() using the base class object and pointer.
Step 9: Create the derived class object and call the functions display() and show() using the derived class object and pointer.




Program Code in C++:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void show()
{
cout<<"\n Base class show:";
}
void display()
{
cout<<"\n Base class display:" ;
}
};
class drive:public base
{
public:
void display()
{
cout<<"\n Drive class display:";
}
void show()
{
cout<<"\n Drive class show:";
}
};
void main()
{
clrscr();
base obj1;
base *p;
cout<<"\n\t P points to base:\n" ;
p=&obj1;
p->display();
p->show();
cout<<"\n\n\t P points to drive:\n";
drive obj2;
p=&obj2;
p->display();
p->show();
getch();
}





Program Output:
P points to Base
Base class display
Base class show
P points to Drive
Base class Display
Drive class Show
no image
Write a C++ program to add and subtract two numbers using polymorphism.




Program Algorithm:
Step 1: Include the header files
Step 2: Declare the base class base
Step 3: Declare and define the function setVar() and getresult()
Step 4: Create a pure virtual function op ()
Step 5: Create two derived classes add and sub from base class
Step 6: Declare the virtual function op ()
Step 7: Create an object pointer for base class
Step 8: Create an object for derived classes and access the member functions to find the result.




Program Code in C++:
#include <iostream.h>
using namespace std;
// abstract base class
class base
{
protected: // attribute section
int num1;
int num2;
int result;
public: // behavior section
void setVar(int n1,int n2)
{
num1 = n1;
num2 = n2;
}
virtual void op() = 0; // pure virtual function
int getresult() 
{
return result;
}
};
class add: public base // add class inherits from base class
{
public:
void op() 
{
result = num1 + num2;
}
};
//sub class inherit base class
class sub: public base
{
public:
void op() 
{
result = num1 - num2;
}
};
int main()
{
int x,y;
base *m; //pointer variable declaration of type base class
add ad; //create object1 for addition process
sub su; //create object2 for subtraction process
cout << "\nEnter two numbers separated by space, or press Ctrl+z to Exit: ";
while(cin >> x >> y)
{
m = &ad;
m->setVar( x , y );
m->op(); //addition process, even though call is on pointer to base!
cout << "\nResult of summation = " << m->getresult();
m = &su;
m->setVar( x , y );
m->op(); //subtraction process, even though call is on pointer to base!
cout << "\nResult of subtraction = " << m->getresult() << endl << endl;
cout << "\nEnter two numbers seperated by space :";
}
return 0;
}





Program Output:
Enter two numbers separated by space: 70 50
Result of summation = 120
Result of subtraction = 20