Professional Documents
Culture Documents
Ex.No:1
Date:
AIM:
To Implement a Function overloading with default arguments in C++
PROGRAM:
output
*********************************************
=============================================
++++++++++++++++++++++++++++++
RESULT:
Thus the program to Implement a Function overloading, default arguments
using C++ was Executed Successfully
----------------------------
Define Simple class with name space
Ex.No:2
Date:
AIM:
To implement a Simple class design with namespaces and objects creations
usingC++
PROGRAM:
#include<iostream>
#include<conio.h>
using namespace std;
namespace mylib{
class student
{
char *name;
int roll;
public:
student(char *tname , int troll)
{
name = tname;
roll=troll;
}
void Disp()
{
cout<<name<<roll;
}
};
}
using namespace mylib;
int main()
{
mylib::student s("rohit", 234);
s.Disp();
getch();
return 0;
}
output
RESULT:
Thus the program to implement a Simple class design with namespaces and
objects creations using C++ was executed successfully.
________________________________________________________________
}
STUDENT(int roll)
{
cout<<endl<<"Constructor 2"<<endl;
rollno=roll;
age=0;perc=0;
}
STUDENT(int roll, int AGE)
{
cout<<endl<<"Constructor 3"<<endl;
rollno=roll;
age=AGE;
perc=0;
}
STUDENT(int roll,float PERC)
{
cout<<endl<<"Constructor 4"<<endl;
rollno=roll;
age=0;
perc=PERC;
}
~STUDENT()
{
cout<<endl<<"destructor at work\t"
<<"roll no : "<<rollno<<endl;
}
};
int STUDENT::input()
{
cout<<endl;
cout<< "Enter Roll number :: ";
cin>>rollno;
cout<<"ENter perc :: ";
cin>>perc;
cout<<"ENter age :: ";
cin>>age;
return 1;
}
int STUDENT::output()
{
cout<<endl;
cout<<endl<<"Roll number\t::\t"<<rollno;
cout<<endl<<"Age\t\t::\t"<<age;
cout<<endl<<"Perc. \t\t::\t"<<perc;
return 1;
}
int main()
{
getch();
clrscr();
STUDENT a=STUDENT();
STUDENT b(12);
STUDENT c(13,66);
STUDENT d(14,5.66F);
STUDENT *e;
e=new STUDENT(34,6);
a.output();
b.output();
c.output();
d.output();
e->output();
cout<<"\ndeleting e"<<endl;
delete(e);
cout<<endl<<"e deleted"<<endl;
return 1;
}
OUTPUT
Constructor 1
Constructor 2
Constructor 3
Constructor 4
Constructor 3
Roll number :: 0
Age :: 0
Perc. :: 0
Roll number :: 12
Age :: 0
Perc. :: 0
Roll number :: 13
Age :: 66
Perc. :: 0
Roll number :: 14
Age :: 0
Perc. :: 5.66
Roll number :: 34
Age :: 6
Perc. :: 0
deleting e
RESULT:
Thus the program to implement the constructor,destructor and Dynamic memory
allocation using C++ was executed successfully.
________________________________________________________________
CopyConstructor and Overloading assignment operator
Ex.No:4
Date:
AIM:
To implement CopyConstructor and Overloading assignment operator usingC+
+
PROGRAM:
#include <iostream.h>
#include<conio.h>
//using namespace std;
class MyClass
{
private:
int data;
public:
MyClass(){ }
MyClass(int d){ data = d; }
MyClass(MyClass& a){
data = a.data;
cout << "\nCopy constructor invoked";
}
void display(){ cout << data; }
void operator = (MyClass& a)
{
data = a.data;
cout << "\nAssignment operator invoked";
}
};
int main()
{ getch();
clrscr();
MyClass a1(37);
MyClass a2;
a2 = a1;
cout << "\na2="; a2.display();
MyClass a3(a1);
output
Assignment operator invoked
a2=37
Copy constructor invoked
a3=37
RESULT:
Thus the program to implement Copy Constructor and Overloading assignment
operator using C++ was executed successfully.
Ex.No:5
Date:
AIM:
To implement Operator overloading using friend function in c++
PROGRAM:
void main()
{
getch();
clrscr();
myclass a(10,20);
myclass b(100,200);
a=a+b;
a.show();
}
Output
110
220
RESULT:
Thus the program to implement Operator overloading with friend function
using C++ was executed successfully
Ex.No:6
Date:
AIM:
To implement Type Conversion concept in c++
PROGRAM:
#include <iostream.h>
#include <string.h>
#include<conio.h>
//using namespace std;
class StringClass {
char str[80];
int len;
public:
StringClass(char *s) {
strcpy(str, s);
len = strlen(s);
}
operator char *() {
return str;
}
operator int() {
return len;
}
};
int main()
{
getch();
clrscr();
StringClass s("Conversion functions are convenient.");
char *p;
int l;
output
The string:
Conversion functions are convenient.
is 36 chars long.
RESULT:
Thus the program to implement “Convert class type to Basic Datatype”
using C++ was executed successfully
Ex.No:7(a)
Date:
AIM:
To implement the Inheritance concept in c++
PROGRAM:
#include <iostream.h>
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout << "Roll Number Is:"<< rollno << "\n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{e=s;}
void put_extra(void)
{ cout << "Extra Score::" << e << "\n";}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
Result:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12
RESULT:
Thus the program to implement Inheritance concept using C++ was executed
successfully.
Ex.No:7(b)
Date:
AIM:
To implement the Inheritance concept with run-time polymorphism
PROGRAM:
#include <iostream.h>
#include<conio.h>
//using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area (void) =0;
void printarea (void)
{ cout << this->area() << endl; }
};
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
int main ()
{
getch();
clrscr();
CPolygon * ppoly1 = new CRectangle;
CPolygon * ppoly2 = new CTriangle;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
ppoly1->printarea();
ppoly2->printarea();
delete ppoly1;
delete ppoly2;
return 0;
}
output
20
10
RESULT:
Thus the program to implement Inheritance concept with run-time polymorphism
using C++ was executed successfully.
template <class T>
class MyClass {
T value1, value2;
public:
MyClass (T first, T second){
value1=first;
value2=second;
}
T getmax ()
{
T retval;
retval = value1>value2 ? value1 : value2;
return retval;
}
};
int main () {
MyClass <int> myobject (100, 75);
cout << myobject.getmax();
return 0;
}
output
100
RESULT:
Thus the program to design a templet using C++ was executed successfully.
Input and OutPut Throwing Exception and Catching exceptions in C++
Ex.No:9
Date:
AIM:
To implement Input and OutPut Throwing Exception and Catching exceptions
in c++
PROGRAM:
#include <iostream.h>
int main()
{
int x,y;
cout << "Enter values of x::";
cin >> x;
cout << "Enter values of y::";
cin >> y;
int r=x-y;
try
{
if ( r!=0)
{
cout << "Result of division is x/r:: " << x/r <<
"\n";
}
else
{
throw ( r);
}
}
catch( int r)
{
cout << "Division by zero exception::
value of r is::" << r << "\n";
}
cout << "END";
return 0;
}
Result:
Enter values of x::12
Enter values of y::12
Division by zero exception::value of r is::0
RESULT:
Thus the program to implement the Input and Output Throwing Exception and
Catching exceptions using C++ was executed successfully.
Standard Template Library
Ex.No:10
Date:
AIM:
To develop Standard Template Library in c++
PROGRAM:
// mathfunc.cpp
// compile with: /EHsc
//
// Structures: plus<A> - Adds data type A object to
// a class object derived from plus.
// minus<A> - Subtracts data type A.
// multiplies<A> - Multiplies object by data type A.
// divides<A> - Divides object by data type A.
// modulus<A> - Returns object modulo A.
#include <functional>
#include <iostream>
#include<conio.h>
/**
* This immutable class represents <i>complex
* numbers</i>.
*
* @author David Flanagan
* @version 1.0
*/
public class Complex {
/**
* Holds the real part of this complex number.
* @see #y
*/
protected double x;
/**
* Holds the imaginary part of this complex number.
* @see #x
*/
protected double y;
/**
* Holds the constant values
* @see #x
* @see #y
*/
private final double z;
/**
* Creates a new Complex object that represents the complex number
* x+yi.
* @param x The real part of the complex number.
* @param y The imaginary part of the complex number.
* @return complex
*/
public Complex(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Adds two Complex objects and produces a third object that represents
* their sum. * @param c1 A Complex object
* @param c2 Another Complex object
* @return A new Complex object that represents the sum of
* <code>c1</code> and
* <code>c2</code>.
* @exception java.lang.NullPointerException
* If either argument is null
*/
public Complex add(Complex c1, Complex c2) {
return new Complex(c1.x + c2.x, c1.y + c2.y);
}
/**
* @deprecated As of release 1.3, replaced by {@link #getPreferredSize()}
*/
@Deprecated public Dimension preferredSize() {
return getPreferredSize();
}
}
output
Package
Class Tree Depr Index Help
ecat
ed
PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
Class Complex
java.lang.Object
Complex
Constructor Summary
Complex(double x, double y)
Creates a new Complex object that represents the complex number x+yi.
Method Summary
Complex add(Complex c1, Complex c2)
Adds two Complex objects and produces a third object that
represents their sum.
Dimension preferredSize()
Deprecated. As of release 1.3, replaced by #getPreferredSize()
Field Detail
x
protected double x
Holds the real part of this complex number.
See Also:
y
y
protected double y
Holds the imaginary part of this complex number.
See Also:
x
Constructor Detail
Complex
public Complex(double x,
double y)
Creates a new Complex object that represents the complex number x+yi.
Parameters:
x - The real part of the complex number.
y - The imaginary part of the complex number.
Method Detail
add
public Complex add(Complex c1,
Complex c2)
Adds two Complex objects and produces a third object that represents their sum. *
@param c1 A Complex object
Parameters:
c2 - Another Complex object
Returns:
A new Complex object that represents the sum of c1 and c2.
Throws:
java.lang.NullPointerException - If either argument is null
preferredSize
@Deprecated
public Dimension preferredSize()
Deprecated. As of release 1.3, replaced by #getPreferredSize()
Package
Class Tree Depr Index Help
ecat
ed
PREV CLASS NEXT CLASS FRAMES NO FRAMES All Classes
SUMMARY: NESTED | FIELD | CONSTR | METHOD DETAIL: FIELD | CONSTR | METHOD
RESULT:
Thus the program to implement Simple class designs with Javadoc using Java
was executed successfully.
RESULT:
Thus the program to implement Interfaces Concept using Java
was executed successfully.
_____________________________________________________________
Inheritance Concept in Java
Ex.No:13(b)
Date:
AIM:
To implement Inheritance in Java
PROGRAM:
class base
{
double a=10,b=20;
void cal()
{
System.out.println("The total Values :"+(a+b));
}
}
class sub extends base
{
double calc(double x,double y)
{
return x*y;
}
}
class sub1 extends sub
{
sub1()
{
System.out.println("This is Sub1 class");
}
sub1(int a)
{
System.out.println("A value si:"+a);
}
}
class DemoInherit
{
public static void main(String[] r)
{
sub1 s=new sub1();
s.cal();
System.out.println(" Mul res:"+s.calc(5.0,6.0));
new sub1();
new sub1(10);
}
}
output
RESULT:
Thus the program to implement Inheritance Concept using Java
was executed successfully.
_____________________________________________________________
Exceptions Handling in Java(ArithmeticException)
Ex.No:14(a)
Date:
AIM:
To implement Exceptions handling in Java
PROGRAM:
/*prgm to illustrate resuming of execution after error reporting */
class errorrepo
{
public static void main(String args[])
{
int a=10,b=5,c=5,x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("Error in denominator value check ");
}
y=a/(b+c);
System.out.println("value of y = "+y);
}
}
output
RESULT:
Thus the program to implement Exceptions handling using Java
was executed successfully.
_____________________________________________________________
Exceptions Handling (ArrayIndexOutofBoundException)
Ex.No:14(b)
Date:
AIM:
To implement Exceptions handling in Java
PROGRAM:
class exec
{
public static void main(String arg[])
{
try
{
int a,b,c;
a=45;b=0;
c=a/b;
int d[]={1};
d[7]=89;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
}
catch(Exception e){
System.out.println(e);}
}
}
output
RESULT:
Thus the program to implement Exceptions handling using Java
was executed successfully.
INPUT AND OUTPUT OPERATION
Ex.No:15
Date:
AIM:
To implement input and output operation in Java
PROGRAM:
import java.io.*;
class ioexample
{
public static void main(String[] args)
{
try {
File ip = new File("input.txt");
File op = new File("output.txt");
FileInputStream fis = new
FileInputStream(ip);
FileOutputStream fos = new
FileOutputStream(op);
int i;
while ((i = fis.read()) != -1)
{
fos.write(i);
}
fis.close();
fos.close();
}
catch (FileNotFoundException fnfe)
{
System.err.println("FileStreamsTest: " +
fnfe);
}
catch (IOException ioe)
{
System.err.println("FileStreamsTest: " + ioe);
} }}
output
input.txt
output.txt
RESULT:
Thus the program to implement input and output operation using Java
was executed successfully.
MultiThreaded Program
Ex.No:16
Date:
AIM:
To implement MultiThreaded Program in Java
PROGRAM:
// Create multiple threads.
class NewThread implements Runnable
{
String name; // name of thread
Thread t;
NewThread(String threadname)
{
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
}
// This is the entry point for thread.
public void run()
{
try
{
for(int i = 5; i > 0; i--)
{
System.out.println(name + ": " + i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(name + "Interrupted");
}
System.out.println(name + " exiting.");
}
}
class MultiThreadDemo
{
public static void main(String args[])
{
new NewThread("One"); // start threads
new NewThread("Two");
new NewThread("Three");
try
{
// wait for other threads to end
Thread.sleep(2000);
}
catch (InterruptedException e)
{
System.out.println("Main thread Interrupted");
}
System.out.println("Main thread exiting.");
}
}
output
RESULT:
Thus the program to implement MultiThreaded Program using Java
was executed successfully.