Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 61

Exp No: 1 Date:

FUNCTION OVERLOADING

AIM: To write a C++ program to implement function overloading.

ALGORITHM: Step 1: Initialize the variables and the functions. Step 2: Declare the same function name with different type of parameters to implement function overloading. Step 3: Call the function using different set of variables and display the output for each reading. Step4: Compile and execute the program.

PROGRAM: #include<iostream.h> #include<conio.h> int volume(int); double volume(double,int); long volume(long,int,int); long volume( ); void areatovol(int ar,int l=5 ) { cout<<"\n\n VOLUME : "<<ar*l; } void main() { clrscr(); cout<<VOLUME = <<volume(10)<<"\n\n"; cout<<VOLUME = <<volume(2.5,8)<<"\n\n"; cout<<VOLUME = <<volume(100,75,15)<<"\n\n"; areatovol(10,6); areatovol(10); getch(); } int volume(int s) {

return(s*s*s); } double volume(double r,int n) { return (3.14*r*r*n); } long volume(long l,int b, int n) { return(l*b*n); }

OUTPUT: VOLUME = 1000 VOLUME = 157 VOLUME = 112500 VOLUME = 60 VOLUME = 50

RESULT: Thus, the program to implement function overloading in C++ is implemented and hence executed.

Exp No: 2 CREATION OF CLASSES AND OBJECTS USING C++ Date: AIM: To design a class in C++ program and to create objects of the class ALGORITHM: STEP 1: create a class and declare the class variables STEP 2: create two objects x and y STEP 3: Get the values for the object variables STEP 4: Print the values of the objects STEP 5: Compile the program and execute it

PROGRAM CODING : #include<iostream.h> #include<conio.h> class item { int number; float cost; public: void getdata(int a,float b) { number=a; cost=b; } void putdata(void) { cout<<"\n\nnumber : \t\t\t"<<number; cout<<"\n\ncost : \t\t\t"<<cost; } }; void main() { item x; clrscr(); cout<<"\n object x"<<"\n"; x.getdata(100,299.95); x.putdata(); item y; cout<<"\n\n object y"<<"\n"; y.getdata(200,175.50);

y.putdata(); getch(); }

OUTPUT: Object X: Number =100 Object Y: Number =200

cost=299.95

Cost=175.50

RESULT: Thus the program to design a class in C++ and to create its objects is executed

Exp no: 3 Date AIM:

STRING CONCATENATION USING DYNAMIC MEMORY ALLOCATION

To write a c++ program to concatenate two strings using dynamic memory allocation. ALGORITHM: STEP 1: Initialize string 1 and string 2. STEP 2: Obtain the string length of strings 1 and 2 and add these lengths to obtain the length of new string . STEP 3: Create a new string by using dynamic memory allocation. STEP 4: Combine string 1 and string 2 and store the concatenated string in string 3. STEP 5: Display the concatenated string.

PROGRAM CODE: #include<iostream.h> #include<string.h> #include<conio.h> void main() { char str1[]="Hello"; char str2[]="Friends"; clrscr();

cout<<"\nthe first string\t"<<str1; cout<<"\nthe second string\t"<<str2; int len=strlen(str1)+strlen(str2); len=len+1; char *str3=new char[len]; for(int i=0;str1[i]!='\0';i++) { str3[i]=str1[i]; } str3[i]='\0'; for(int j=0;str2[j]!='\0';j++) { str3[i]=str2[j]; i++; } str3[i]='\0'; cout<<"\n the string after concatenation\t"<<str3; getch(); } OUTPUT: The first string Hello The second string Friends The string after concatenation HelloFriends

RESULT: Thus the c++ program to concatenate two strings using dynamic memory allocation is executed successfully

Exp no:4 Date : ARITHMETIC OPERATIONS ON COMPLEX NUMBERS

AIM: To implement arithmetic operations on complex numbers using operator overloading

ALGORITHM: Step1: Start the program. Step2: Create a class and declare variables and functions . Step3: Declare the operator functions. Step4: Perform arithmetic calculations by calling operator functions. Step5: Execute the corresponding output. Step6: Stop the program.

PROGRAM:

#include<iostream.h> #include<conio.h> class complex { public: float a,b,c,d; float x,y,f,g; void getdata(); void operator+(); void operator-(); void operator++(); void operator(); }; void complex::getdata() { cout<<Enter the value of complex number a and b in a+bj:; cin>>a>>b; cout<<Enter the value of another complex number c and d in c+dj:; cin>>c>>d; cout<<a+jb=<<a<<+j<<b<<\n; cout<<c+jd=<<c<<+j<<d<<\n;

} void complex ::operator+() { x=a+c; y=b+d; cout<<The sum of the two complex number is :<<x<<+j<<y<<\n; } void complex::operator() { f=a-c; g=b-d; cout<<The subtraction of two complex number is<<f<<+j<<g<<\n; } void complex::operator++() { Float re,im; re=(a*c)-(b*d); im=(a*d)+(b*c); cout<<The multiplication of two complex number is<<re<<+j<<im<<\n; } void complex::operator()

{ float re,im; re=((a*c)+(b*d))/((c*c)+(d*d)); im=((b*c)-(a*d))/((c*c)+(d*d)); cout<<The division of the two complex number is <<re<<+j<<im<<\n; } Void main() { complex c; clrscr(); c.getdata(); +c; -c; ++c; --c; getch(); }

Result: Thus the c++ program to implement arithmetic operation on complex number using operator overloading has been written and executed successfully.

Exp no:5 Date : AIM: To write a c++ program to display the transaction details using friend function. ALGORITM: STEP 1: Create a class named accdetails with member function getdata and a friend function transactions. STEP 2: Get the name of the account holder and the balance amount using getdata function. STEP 3: Inside the transactions function enter the choice to either credit or debit the amount from the existing balance. STEP 4: Display the amount. PROGRAM CODE: #include<iostream.h> #include<conio.h> class accdetails { private: char name[25]; float amount; float deb,cre; public: ACCOUNT DETAILS USING FRIEND FUNCTION

void getdata(); friend int transactions(accdetails x); }; void accdetails::getdata() { cout<<"enter the name of the account holder"; cin>>name; cout<<"enter the amount"; cin>>amount; } int transactions(accdetails x) { int c; cout<<"enter your choice 0 or 1"; cin>>c; switch(c) { case 0: cout<<"enter the debit amount"; cin>>x.deb; x.amount=x.amount-x.deb; cout<<"\namount in the savings"<<x.amount; break; case 1:

cout<<"enter the credit amount"; cin>>x.cre; x.amount=x.amount+x.cre; cout<<"\namount in the savings"<<x.amount; break; } return 0; } void main() { accdetails obj; clrscr(); obj.getdata(); transactions(obj); getch(); } OUTPUT: enter the name of the account holder RUCHEET enter the amount 20000 enter your choice 0 or 1 0 enter the debit amount 2000 amount in the savings18000

RESULT: Thus the c++ program to display the transaction details using friend function is executed successfully.

Exp no:6 Date :

MULTIPLE INHERITANCE

AIM: To write a C++ program to implement multiple inheritance . ALGORITHM: STEP:1 Create two base classes named first and second . STEP:2 Obtain the data to be displayed using get data function . STEP:3 Create a derived class named third which derives its data from the base classes first and second. STEP:4 Display the data using putdata function.

PROGRAM: #include<iostream.h> #include<conio.h> class first { private: char bname[25]; int isbn; public: void getdata() { cout<<"enter the book name";

cin>>bname; cout<<"enter the book number";

cin>>isbn; } void putdata() { cout<<"book name"<<bname<<endl; cout<<"book number"<<isbn<<endl; } }; class second { private: char aname[25]; char pub[25]; public: void getdata() { cout<<"enter the author name"; cin>>aname; cout<<"enter the publishers name"; cin>>pub; }

void putdata() { cout<<"author name"<<aname<<endl; cout<<"publishers"<<pub<<endl; } }; class third:public first,public second { public: void getdata() { first::getdata(); second::getdata(); } void putdata() { first::putdata(); second::putdata(); } }; void main() { third ob; clrscr();

ob.getdata(); ob.putdata(); getch(); }

OUTPUT: enter the book name PE enter the book number 123 enter the author name BHIMBRA enter the publishers name KHANNA book name PE book number 123 author name BHIMBRA publishers KHANNA

RESULT: Thus the program to implement multiple inheritance is executed successfully.

Exp no:7 Date : HYBRID INHERITANCE

AIM: To write an object oriented program to implement the concept of Hybrid inheritance. ALGORITHM: STEP 1: STEP 2: STEP 3: STEP 4: STEP 5: STEP 6: STEP 7: STEP 8: Start the program Define a class a with some member functions Define a class b as a child of class a with a member functions Define a class c as a child of class a with a member functions Define a class d as a child of both class b and class c with a member function Write the main function by creating object for the class d. Access the members of all other classes using that object Stop the program

SOURCE CODE: #include<iostream.h> #include<conio.h> class a { public: char name[30]; void getname() { cout<<"\nname:"; cin>>name; } }; class b:virtual public a { public: int rno; void getrno() { cout<<"\nrno:"; cin>>rno; } }; class c:virtual public a { public: int m1,m2; void gettmarks() { cout<<"\nmarks:"; cin>>m1; cin>>m2; } };

class d:public b,public c { public: int age; int total; void gettotal() { total=m1+m2; } void dis() { cout<<"\nName:"<<name<<"\nRoll.No:"<<rno<<"\nTotal Marks:"<<total; } }; void main() { clrscr(); d d1; d1.getname(); d1.getrno(); d1.gettmarks(); d1.gettotal(); d1.dis(); getch(); }

SAMPLE OUTPUT: Name: xxx r.no: 23 marks: 40 40

Name: xxx r.no: 23 marks: 80

RESULT: Thus an object oriented program is written to implement the concept of hybrid inheritance.

Exp no:8 Date : AIM: To implement a C++ program to sort the given array of numbers using class template. SORTING USING TEMPLATES

ALGORITHM: STEP1: Create a class template and declare variables and functions. STEP2: Create an instance for the class and execute various function calls. STEP3: Get the array size and the array contents. STEP4: Sort the array using for loops. STEP5: Display the sorted array. STEP6: Compile and execute the program.

PROGRAM: #include<iostream.h> #include<conio.h> template<class T> class sort { private:T i,j; T s;

T a[5]; public: void getdata() { cout<<"\nenter the size:"; cin>>s; cout<<"\nenter the nos:\t"; for(i=0;i<s;i++) cin>>a[i]; } void sorting() { T temp; for(i=0;i<s-1;i++) { for(j=i+1;j<s;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } }

} } void display() { for(i=0;i<s;i++) cout<<a[i]<<"\t"; } }; void main() { clrscr(); sort<int> obj; sort<float> o; obj.getdata(); obj.sorting(); obj.display(); o.getdata(); o.sorting(); o.display(); getch(); }

OUTPUT: Enter the size:3 Enter the nos:4 6 1 1 4 6 Enter the size:3 Enter the nos:8.3 1.7 6.3 1.7 6.3 8.3

RESULT: Thus the C++ program to sort the given array was written and executed successfully.

Exp no:9 Date : RUNTIME POLYMORPHISM (VIRTUAL FUNCTIONS)

AIM: To write an object oriented program to implement the concept of Runtime polymorphism using c++. ALGORITHM: Step 1: Start the program Step 2: Create a class named media with some members and member functions Step 3: Create a blank virtual display function within this class Step 3: Create a class book as a derived class of class media with some member functions and a display function Step 4: Create a class named tape as a derived class of class media with a display function Step 5: In the main function get the values for the variables of each class and call the member functions from the main function Step 6: Stop the program

SOURCE CODE: #include<iostream.h> #include<conio.h> #include<string.h> class media { protected: char title[50]; float price; public: media(char *s,float a) { strcpy(title,s); price=a; } virtual void display(){} }; class book: public media { int pages; public: book(char *s,float a,int p):media(s,a) { pages=p; } void display(); }; class tape:public media { float time; public: tape(char *s,float a,float t):media(s,a) { time=t;

} void display(); }; void book :: display() { cout<<"\nTITLE: "<<title; cout<<"\nPAGES: "<<pages; cout<<"\nPRICE: "<<price; } void tape::display() { cout<<"\nTITLE: "<<title; cout<<"\nPLAY TIME: "<<time<<"mins"; cout<<"\nPRICE: "<<price; } void main() { clrscr(); char *title=new char[30]; float price,item,time; int pages; cout<<"\nENTER BOOK DETAILS\n"; cout<<"TITLE:";cin>>title; cout<<"PRICE:";cin>>price; cout<<"PAGES:";cin>>pages; book b1(title,price,pages); cout<<"\nENTER TAPE DETAILS\n"; cout<<"TITLE:";cin>>title; cout<<"PRICE:";cin>>price; cout<<"PLAY TIME:";cin>>time; tape t1(title,price,time); media *list[2]; list[0] = &b1; list[1] = &t1; cout<<"\nMEDIA DETAILS";

cout<<"\nBOOK"; list[0]->display(); cout<<"\nTAPE"; list[1]->display(); getch(); } SAMPLE OUTPUT: Enter Book Details: Name: msk Price: $45 Pages: 345 Enter Tape details: Name: msk Price: $45 Time: 90 Media Details Book Name: msk Price: $45 Pages: 345 Tape Name: msk Price: $45 Time: 90

RESULT: Thus an Object oriented program is written to implement the concept of runtime polymorphism using C++.

Exp no:10 Date : EXCEPTION HANDLING

AIM: To write an object oriented program to implement the concept of exception handling using C++. ALGORITHM: Step 1: Step 2: Step 3: Step 4: performed Step 5: performed Step 6: Step 7: Start the program Get values for two integer variables Store the difference between them in the third variable In the try block write the required operation to be In the catch block write the required operation to be Write a proper throw block before the catch block. Stop the program

SOURCE CODE: #include<iostream.h> #include<conio.h> void main() { int a,b,x; cout<<"A= ";cin>>a;cout<<"\nB= ";cin>>b; x=a-b; try { if(x!=0) { cout<<"A / X = "<<a/x; } else { throw(x); } } catch(int x) { cout<<"EXCEPTION! X="<<x; cout<<"ERROR!\n A CANNOT BE EQUAL TO B"; break; } getch(); }

SAMPLE OUTPUT: A= 25 B= 25 EXCEPTION! X = 0 ERROR! A CANNOT BE EQUAL TO B

RESULT: Thus an object oriented program is written to implement the concept of exception handling using C++.

Exp no:11 Date : STANDARD TEMPLATE LIBRARY AIM: To write an object oriented program to implement the concept of Standard Template Library using C++. ALGORITHM: Step 1: Start the program Step 2: Include the header file vector Step 3: Create a display function with vector variable as arguments Step 4: In main function get five integers and call the push back function each time Step 5: Have a function to display the current contents of the array Step 6: Insert any number into the array and call the display function Step 7: Delete a number from the array and display after deletion Step 8: Stop the program.

SOURCE CODE: #include<iostream.h> #include<conio.h> #include<vector> void display(vector<int>&v) { for(int i=0;i<v.size();i++) { cout<<v[i]<<" "; } } int main() { vector<int> v; cout<<"Initial Size"<<v.size(); int x; cout<<"Enter 5 integer values"; for(int i=0;i<5;i++) { cin>>x; v.push_back(x); } cout<<"Size after adding 5 values: ";cout<<v.size(); cout<<"Current Contents: ";display(v); v.pushback(6.6); cout<<"Insize: "<<v.size(); cout<<"Contents Now: ";display(v); vector<int>::iterator itr:v.begin(); itr=itr+3; v.insert(itr,1,9); cout<<"Its content after inserting";display (v); v.erase(v.begin()+3,v.begin()+5) { cout<<"Contents after deletion: ";display(v);

cout<<"END"; } getch(); }

SAMPLE OUTPUT: Initial Size: 5 Enter 5 integer values: 2 3 4 5 6 Current Contents: 2 3 4 5 6 Intsize: 5

Its content after inserting 2 3 4 5 6 6.6 Contents after deletion: 4 5 6 6.6

RESULT: Thus an object oriented program is written to implement the concept of Standard template library using C++.

Exp no:12 Date : RANDOM NUMBER GENERATION IN JAVA

AIM: To generate random numbers using simple Java program. ALGORITHM: STEP 1: Declare the class . STEP 2: Declare the public static main function inside the class. STEP 3: Define the function with necessary statements. STEP 4: Print the output using the command System.out.println STEP 5: Compile and execute the program.

PROGRAM: class rand { public static void main(String args[]) { double ra; int I ; System.out.println("Random Numbers are:"); for (i= 1;i<=10;i++) { ra=Math.random(); System.out.println("\t"+ra); } } }

COMPILATION AND EXECUTION: D:\javaprg>javac rand.java D:\javaprg>java rand

OUTPUT: Random Numbers are: 0.7178800297173848 0.10272303466350396 0.09880382903038853 0.20521914937736818 0.4946446248062951 0.7236876729590774 0.06362232446845784 0.3103631103262836 0.986773064331572 0.888041209983093 RESULT: Thus, the program to generate random numbers using JAVA is compiled and executed.

Exp no:13 Date : STUDENTS DETAILS USING INTERFACE

AIM: To write a program in JAVA to print the roll number and marks of the student using inheritance.

ALGORITHM: STEP 1: Declare the class. STEP 2 Declare the required variables and assign them a value if necessary. STEP 3: Declare and define a function to print the output on the screen. STEP 4: Declare a second class and inherit the first class in it. STEP 5: Declare the main function and provide the values for the roll number and marks of the student. STEP 6: Compile and execute the program.

PROGRAM: class student { int rollnumber; void getNumber(int n) { rollnumber=n; } void putnumber( ) { System.out.println(Roll no : + roll number); } } class test extends student { float part1, part2; void getmarks(float m1,float m2) { part1=m1; part2=m2; } void putmarks( ) { System.out.println(marks obtained);

System.out.println(part 1=+part 1); System.out.println(part 2=+part 2); } } interface sports { float sportwt=6.0f; void putwt(); } class Results extends test implements sports { float total; public void putwt() { System.out.println( sport wt=+sportwt); } void display ( ) { total=part1+part2+part3); putnumber(); putmarks(); putwt(); System.out.println(Total score=+total); }

} class hybrid { public static void main(String args[]) { results student1=new results(); student1.getnumber(1234); student1.getmarks(27.5f,33.0f); student1.display(); }

OUTPUT: ROLL NO: 12324 MARKS OBTAINED: PART1=27.5 PART2=33 SPORTS WT=6 TOTAL SCORE=66.5

RESULT: Thus the program to print the roll number and marks of the student using inheritance is executed

Exp no:14 Date : PROGRAM FOR DEVELOPING PACKAGES

AIM: To write a program in JAVA for developing packages.

ALGORITHM: STEP 1: Declare the package with the package name starting with a small letter. STEP 2: Define the class as public. STEP 3: Create a sub directory under the directory where the main source files are stored. STEP 4: Compile the Files that are stored in the subdirectory.

PROGRAM: Package mypack; Public class balance { String name; Double bal; Public balance(string n, doube b) { Name =n; Bal=b; } Public void show() { If(bal<0) System.out.println(); Else System.out.println(name+:$+bal); } } Import mypack.*; Class testbalance { Public static void main(string args[]) {

Balance t[]=new balance[3]; T[0]=new balance(mr.x,123.23); T[1]=new balance(mr.y,157.02); T[2]=new balance(mr.z,-12.33); For(int i=0;i<3;i++) T[i].show();}} OUTPUT: MR.X:$123.23 MR.Y : $157.02 MR.Z :

RESULT: Thus, the program to implement packages in JAVA is compiled and hence executed

Exp no:15 Date : PROGRAM USING MULTITHREADING

AIM: To write a program in JAVA to implement multi-threading. ALGORITHM: Step 1: Declare a class and create a new thread. Step 2: Create the second thread and start it. Step 3: Declare the main function. Step 4: Create the new thread and initialize the value to print the necessary outpit. Step 5: Compile and execute the program.

PROGRAM: class NewThread extends Thread { NewThread() { // Create a new, second thread super("Demo Thread"); System.out.println("Child thread: " + this); start(); // Start the thread } // This is the entry point for the second thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); // Let the thread sleep for a while. Thread.sleep(500); } } catch (InterruptedException e) { System.out.println("Child interrupted."); } System.out.println("Exiting child thread."); } } class ExtendThread { public static void main(String args[]) {

new NewThread(); // create a new thread try { for(int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(1000); }} catch (InterruptedException e) { System.out.println("Main thread interrupted."); } System.out.println("Main thread exiting."); OUTPUT: Child thread: Thread[Demo Thread,5,main] Main Thread: 5 Child Thread: 5 Child Thread: 4 Main Thread: 4 Child Thread: 3 Child Thread: 2 Main Thread: 3 Child Thread: 1 Exiting child thread. Main Thread: 2 Main Thread: 1 Main thread exiting. RESULT: Thus, the program to implement multi-threading is executed using JAVA.

Exp no:16 Date : PROGRAM USING EXCEPTION HANDLING IN JAVA

AIM: To write a program in JAVA to implement exception handling.

ALGORITHM: Step 1: Import all the library files of JAVA. Step 2: Declare the class and the required variables inside the class . Step 3: Declare the condition for the exception inside a function Step 4: Declare the main function Step 5: Handle the exception inside the main and print the necessary output. Step 6: Compile and execute the program.

PROGRAM: import java.io.*; public class CheckingAccount { private double balance; private int number; public CheckingAccount(int number) { this.number = number; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) throws InsufficientFundsException { if(amount <= balance) { balance -= amount; } else { double needs = amount - balance; throw new InsufficientFundsException(needs); } } public double getBalance() { return balance; } public int getNumber() {

return number; // File Name BankDemo.java public class BankDemo { public static void main(String [] args) { CheckingAccount c = new CheckingAccount(101); System.out.println("Depositing $500..."); c.deposit(500.00); try { System.out.println("\nWithdrawing $100..."); c.withdraw(100.00); System.out.println("\nWithdrawing $600..."); c.withdraw(600.00); }catch(InsufficientFundsException e) { System.out.println("Sorry, but you are short $" + e.getAmount()); e.printStackTrace(); } } } }

OUTPUT Depositing $500... Withdrawing $100... Withdrawing $600... Sorry, but you are short $200.0 InsufficientFundsException at CheckingAccount.withdraw(CheckingAccount.java:25) at BankDemo.main(BankDemo.java:13)

RESULT: Thus, The program to implement exception handling is implementer in JAVA

Exp no:17 Date : PROGRAMS IN JAVA FOR INPUT/OUTPUT

AIM: To write a program in JAVA for input and output. ALGORITHM: Step 1: Declare the main function inside the class. Step 2: Get the input using the Inputstream. Step 3: Assign the output using the Output stream. Step 4: Print the output. Step 5: Compile and Execute the program.

PROGRAM: import java.io.*; public class fileStreamTest{ public static void main(String args[]) { Try { byte bWrite [] = {11,21,3,40,5}; OutputStream os = new FileOutputStream("C:/test.txt"); for(int x=0; x < bWrite.length ; x++){ os.write( bWrite[x] ); // writes the bytes } os.close(); InputStream is = new FileInputStream("C:/test.txt"); int size = is.available(); for(int i=0; i< size; i++){ System.out.print((char)is.read() + " "); } is.close(); } catch(IOException e){ System.out.print("Exception"); }

OUTPUT: 11 21 3 40 5

RESULT: Thus, the program using input and output stream is implemented in JAVA.

You might also like