FUNCTION OVERLOADING AIM: To write a C++ program to perform function overloading. ALGORITHM: Step1: Start the program. Step2: Define a class with necessary data items and member function with same function name and different number and types of arguments. Step3: Define a member function to calculate the volume of different shapes. Step4: Define the main function . Step5: Call the member function using the object created. Step6: Print the result. Step7: Stop the program. PROGRAM: #include<iostream.h> #include<conio.h> class overloading CS2312-OBJECT ORIENTED PROGRAMMING LAB
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
FUNCTION OVERLOADING
AIM:To write a C++ program to perform function overloading.
ALGORITHM:
Step1: Start the program.
Step2: Define a class with necessary data items and member function with same
function name and different number and types of arguments.
Step3: Define a member function to calculate the volume of different shapes.
Step4: Define the main function.
Step5: Call the member function using the object created.
int r,c;clrscr();cout<<"\n Enter rows and cols of the matrix….\n";cin>>r>>c;matrix m1(r,c);cout<<"\n Enter the matrix elements one by one….\n";m1.getmatrix();cout<<"\n Entered matrix is….\n";m1.showmatrix();matrix m2=m1;cout<<"\nResult of copy constructor is….\n";m2.showmatrix();
CS2312-OBJECT ORIENTED PROGRAMMING LAB
matrix m3;m3=m1;cout<<"\n Result of assignment operator overloading.\n";m3.showmatrix();getch();
}
CS2312-OBJECT ORIENTED PROGRAMMING LAB
OUTPUT:
Enter the rows and cols of the matrix….3 3
Enter the matrix elements one by one….
123456789
Entered matrix is….
1 2 34 5 67 8 9
Copy Constructor invoked….
Result of copy constructor is….
1 2 34 5 67 8 9
Assignment Operator overloading….
Result of assignment operator overloading….
1 2 34 5 6
CS2312-OBJECT ORIENTED PROGRAMMING LAB
7 8 9
OPERATOR OVERLOADING USING FRIEND FUNCTION
AIM:To write a C++ program to import operator overloading with friend function concept.
ALGORITHM:Step1: Start the program.
Step2: Create a class complex with 3 constructor to perform constructor
overloading. The first constructor takes no argument which is used to
create objects which are not initialized.
Step3: The second takes one argument which is used to create object and
initialize them to specific value.
Step4: Overload the arithmetic operators using friend function to perform
arithmetic operation on the complex number.
Step 5: Stop the program
PROGRAM:
#include<iostream.h> #include<conio.h> #include<math.h> class complex {
AIM:To write a C++ program to implement necessary operator overloading and
type conversion.
ALGORITHM:
Step1: Start the program.Step2: Invoke the parameterized constructor complex for x, y, z values.Step 3: Create object for complex class.Step 4: Copy the values from x to u using complex u(x) function.Step 5: Again copy the values from x to v using complex v=x.Step 6: Again copy the values from x to w and to a.Step 7: Print the real and imaginary values.Step 8: Stop the program.
clrscr();complex a;complex x(1.0,2.0),y(20.0),z(11.0,12.0);complex u(x);complex v=x;complex w=x+y-z;a=x+y-z;cout<<"The real part of complex number: "<<double(a)<<endl;cout<<"The imaginary part of complex number: "<<exp(a)<<endl;getch();
}
OUTPUT:
The real part of complex number: 10
CS2312-OBJECT ORIENTED PROGRAMMING LAB
The imag part of complex number: 220226.465795
INHERITANCE
AIM:To write a C++ program to implement bank details using inheritance
concept.
ALGORITHM:
Step1: Start the program.
Step2: Create a bank class.
Step3: Create a subclass savings which inherits properties from the bank
class and current class that inherits from savings class.
Step4: The savings class defines member function to get details about the
deposit and withdrawal amount and update balance information for
current account.
Step5: Write a member function to get deposit amount and to update
balance information for current account.
Step6: Write a member function to display the balance information for
saving account.
Step7: Write a member function to display the balance information for
cout<<"Enter the amount to be deposited=Rs.";cin>>dep;bal+=dep;cout<<"\nEnter the amount to be withdrawn=Rs.";cin>>wdraw;bal-=wdraw;cout<<"\nYour account balance is=Rs."<<bal;
int ch;current a;clrscr();cout<<"Enter customer name: ";cin>>a.cname;cout<<"\n Enter your A/c no.:";
CS2312-OBJECT ORIENTED PROGRAMMING LAB
cin>>a.accno;cout<<endl;while(1){
cout<<"Enter the A/c type: 1.Savings 2.Current 3.Exit\n";cin>>ch;switch(ch)
{case 1:
a.saves();break;
case 2:a.info();break;
case 3:break;
default:cout<<"Invalid choice";break;
}if(ch==3)break;}getch();
}
OUTPUT:Enter customer name:xxxEnter your A/c no.:566Enter the A/c type:1.Savings 2.Current 3.Exit1Enter the amount to be deposited=Rs.5000Enter the amount to be withdrawn=Rs.500Your A/c balance is=Rs.4500Enter the A/c type:1.Savings 2.Current 3.Exit2Last amount withdrawn=Rs.500Last amount deposited=Rs.5000Your account balance is=Rs.4500
CS2312-OBJECT ORIENTED PROGRAMMING LAB
Enter the A/c type:1.Savings 2.Current 3.Exit3
RUNTIME POLYMORPHISM USING VITUAL FUNCTION
AIM: To write a C++ program to calculate the area of triangle and rectangle
using derived classes and display the result using virtual function. ALGORITHM:
Step1: Start the program.
Step2: Create a base class shape.
Step3: Derive two subclasses triangle and rectangle from the base class
shape.
Step4: Derive member function get_data() and display_area().
Step5: Find out the area of triangle and rectangle and display the result
TriangleEnter the height and base of triangle = 4 6Area of triangle=12 sq.unitsRectangleEnter the length and breadth of rectangle = 7 8Area of rectangle=56 sq.units
CS2312-OBJECT ORIENTED PROGRAMMING LAB
TEMPLATE DESIGN IN C++
AIM: To write a C++ program to print the Student Details.
ALGORITHM:
Step1: Start the program.
Step2: Assign a variable n and a character name[20].
cout<<"\nEnter the details of student\t"<<a;t no,m1,m2,m3,avg,tot;cout<<"\nEnter the no.";cin>>no;cout<<"\nEnter marks in three subjects";cin>>m1>>m2>>m3;tot=m1+m2+m3;avg=tot/3;cout<<"\nResult of No."<<no;cout<<"\nm1="<<m1<<"\tm2="<<m2<<"\tm3="<<m3;cout<<"\ntotal\t"<<tot;cout<<"\navg\t"<<avg;
}void main(){
CS2312-OBJECT ORIENTED PROGRAMMING LAB
clrscr();int n;char name[20];cout<<"Enter the no of students to get:\t";cin>>n;for(int i=1;i<=n;i++){
cout<<"\nEnter the student name:\t";cin>>name;get(i);cout<<"\nName"<<name;
}getch();
}
OUTPUT:Enter the no of students to get 2Enter the student name : RajEnter the details of student 1Enter the no.20Enter the marks in three subjects 90 80 100
Result of No. 20m1=90 m2=80 m3=100total 270avg 90Name Raj
Enter the no of students to get 2Enter the student name : SujiEnter the details of student 1Enter the no.20Enter the marks in three subjects 100 100 70
Result of No. 20m1=100 m2=100 m3=70total 270avg 90Name Suji
CS2312-OBJECT ORIENTED PROGRAMMING LAB
HANDLING I/O
AIM: To write a C++ program to find the square root of a number and to format
the display.
ALGORITHM:
Step1: Start the program.
Step2: cout.width( ) specifies the required field size for displaying the output.
Step3: cout.precision( ) specifies the number of digits to be displayed after the
decimal point of a floating point number.
Step4: cout. fill( ) specifies a character to be filled is unused portion of the
field.
Step5: cout.setf(ios::left,ios::adjustfield) Left justification output.
Step6: cout.setf(ios::right,ios::adjustfield) Right justification output.
Step7: cout.setf(ios::showpoint) shows trailing decimal point and zeros.
Step8: cout.setf(ios::showpos) prints + before positive integers.
Step 9: cout.setf(ios::fixed,ios::float field) use ordinary floating point notation.
Step10: cout.setf(ios::scientific,ios::float field) use exponential floating point
notation.
Step11: cout.setf(ios::internal,ios::float field) paddding occurs between
sign/base indicator and the number, when the number output failsto fill
int i;clrscr();cout.fill('*');cout.setf(ios::left,ios::adjustfield);cout.width(10);cout<<"VALUE=";cout.setf(ios::right,ios::adjustfield);cout.width(15);cout<<"SQUARE ROOT OF\n";cout.fill('.');cout.precision(4);cout.setf(ios::showpoint);cout.setf(ios::showpos);cout.setf(ios::fixed,ios::floatfield);for(i=1;i<=10;i++){cout.setf(ios::internal,ios::adjustfield);cout.width(5);cout<<i;cout.setf(ios::right,ios::adjustfield);cout.width(20);cout<<sqrt(i)<<"\n";
cout<<”\n Trying to refer a[1]=”;a[1]=10;cout<<”succeeded”;cout<<”\n Trying to refer a[15]”;a[15]=10;cout<<”succeeded “;
}catch (array::RANGE){
cout<<”cout of range in array reference”;}
}
OUTPUT:
Maximum array size allowed=10Trying to refer a[1]=SucceededTrying to refer a[15]=Out of range in array reference
CS2312-OBJECT ORIENTED PROGRAMMING LAB
PROGRAM DEVELOPMENT USING STL
AIM:To write a C++ program to declare a list and to perform some operations
using STL.
ALGORITHM:Step1: Create an empty list lst to hold integers.
Step2: Get the number of elements to insert in the list.
Step3: Using push_back() insert the elements in the list.
Step4: Access the contents of the list by declaring and initializing iterator.
Step5: Using sort() function sort the list.
Step6: Display the sorted list.
Step7: Modify the elements of the list by adding 1000 to each element.
Step8: Display the modified list.
PROGRAM:
#include<iostream.h>#include<list.h>void main(){
list<int>lst;int i,n,item;cout<<”\n How many elements you want to insert?”<<endl;cin>>n;cout<<”\nEnter the elements in the list”<<endl;for(i=0;i<n;i++){
cin>>item;lst.push_back(item);
}cout<<”The size of the list is= “<<lst.size()<<endl;
CS2312-OBJECT ORIENTED PROGRAMMING LAB
cout<<”The contents of the list are: “<<endl;list<int>::iterator ptr=lst.begin()while(ptr!=lst.end()){
cout<<*ptr<<” “;ptr++;
}lst.sort();cout<<”\nSorted elements of the list are:”<<endl;ptr=lst.begin();while(ptr!=lst.end()){
cout<<*ptr<<” “;ptr++;
}ptr=lst.begin();while(ptr!=lst.end()){
*ptr=*ptr+1000;ptr++;
}cout<<”\nModified elements of the list are:”<<endl;ptr=lst.begin();while(ptr!=lst.end()){
cout<<*ptr<<” “;ptr++;
}cout<<”\n\n”;
}
CS2312-OBJECT ORIENTED PROGRAMMING LAB
OUTPUT:
How many elements you want to insert?5Enter the elements of the list3010405020The size of the list is=5The contents of the list are:30 10 40 50 20Sorted elements of the list are:10 20 30 40 50Modified elements of the list are:1010 1020 1030 1040 1050
CS2312-OBJECT ORIENTED PROGRAMMING LAB
SIMPLE CLASS DESIGN IN JAVAAIM:
To write a program depicting simple class design using JAVA.
ALGORITHM:Step1: Start the program.
Step2: Define main method inside the class specification.
Step3: Define three string variables.
Step4: Display the length of the strings.
Step5: Display the character of index three of first string.
Step6: Compare all Strings and print the result.
Step7: Stop the program.
PROGRAM:class basic{
public static void main(String args[]){
String strob1="Sriram";String strob2="ram";String strob3=strob1;System.out.println("lengthof String1="+strob1.length());System.out.println("lengthof String2="+strob2.length());System.out.println("character at 3rd position="+strob1.charAt(3));if(strob1.equals(strob2))
AIM:To write a java program for suspend,resume and stopping threads.
ALGORITHM:Step1: Create a class that implements Runnable interface to handle threads.Step2: Create two threads that shares the CPU.Step3: Using start() method start the thread execution which begins at the run method.Step4: Using suspend() and resume() methods defined by thread pause and restart
the execution of thread.Step5: Using stop() method to stop the thread after finishing its execution.PROGRAM: