mca-ii sem-mca2610-prog lab-iii-oops and c++ manual

68
1. QUADRATIC EQUATION USING CONSTRUCTOR Aim: To write a c++ program for problem number and using constructor to initialize the number. Algorithm: Step1: Start the program. Step2: Create the class and declare the required number variable and member function. Step3: Initialize the data member using constructor and assign value. Step4: Using appropriate function calculates the simple interest and display. Simple interest using display() function. Step5: In the main() function create the object and using object call the method of the class. Step6: Stop the program.

Upload: sowmyamukhamisrinivasan

Post on 14-Oct-2015

47 views

Category:

Documents


0 download

DESCRIPTION

2nd sem MCA program lab

TRANSCRIPT

  • 1. QUADRATIC EQUATION USING CONSTRUCTORAim:

    To write a c++ program for problem number and using constructor to initialize thenumber.Algorithm:Step1: Start the program.Step2: Create the class and declare the required number variable and member function.Step3: Initialize the data member using constructor and assign value.Step4: Using appropriate function calculates the simple interest and display.

    Simple interest using display() function.Step5: In the main() function create the object and using object call the method of the class.Step6: Stop the program.

  • //Quadratic Equation#include#include#includeclass quad{private:float a,b.c,d,D,x1,x2,image,real;public:void get(){coutb>>c;}void cal(){D=(b*b)-(4*a*c);if(D==0){cout
  • x1=(-b+sqrt(D))/(2(*a);x2=(-b-sqrt(D)/(2*a);cout
  • Sample input and output:ENTER THE A, B, C VALUES:121THE ROOTS ARE REAL AND EQUAL:132THE ROOTS ARE IMAGINARYTHE ROOTS ARE:-1+1.732051i-1-1.732051iResult:

    Thus the above program has been successfully executed.

  • 2. A SIMPLE PROGRAM USING CLASS AND OBJECTSAim:

    To write a C++ program to initialize a member function using class and objects.Algorithm:Step1: Start the program.Step2: Create a class and declare the required variable in private section.Step3: In public create a necessary function to get the inputs, apply the

    function to print the values.Step4: In main function create an object and call the class.Step5: Stop the program.

  • // Simple Class and Object#include#includeclasssi{private:int p,n,r,si;public:void get(){coutp;coutn;coutr;}void cal(){si=(p*n*r)/100;}void disp(){cout
  • Enter the rate of interest 2Si=2

    Result:Thus the above program was successfully executed and the output was verified.

    3. PRIVATE MEMBER FUNCTIONAim:

    To write a c++ program to implement a private member function.Algorithm:Step1: Start the program.Step2: Create a class and declare a required variable and require a design member function asprivate.Step3: In public function create a static member function to call the private function.Step4: To display the required output.Step5: Create a class object using main().Step6: Stop the program.

  • // Private Member Function#include#includeclass simple{private:float l,b,area;void read(){coutl>>b;}public:void sd(){read();}void call();{

  • area=l*b;}void disp(){cout
  • 4(a).OVERLOADING A BINARY OPERATOR TO PERFORMADDITION OF TWO COMPLEX NUMBERS.

    Aim:To write a C++ program to overloading binary operator to perform adding of complex

    number.Algorithm:Step1: Start the program.Step2: To declare the class name complex and define the function.Step3: And then read operation and calculate the values.Step4: Display the value by using the void display() function.Step5: Then to create a object and utilities the value.Step6: Stop the program.

  • //Operator Overloading#include#includeclass complex{float x,y;public:complex(){}complex(float real,float image){x=real;y=imag;}friend complex operator+(complex,complex);void display(();};complex operator+(complex c,complex d){complex temp;temp.x=c.x+d.x;temp.y=c.y+d.y;return(temp);

  • }void complex::display(){cout
  • 4(b).OVERLOADING A BINARY OPERATOR TO PERFORMTHE OPERATION CONCATINATION OF TWO STRINGS

    Aim:To write a C++ program to overloading binary operator to perform adding of complex

    number.Algorithm:Step1: Start the program.Step2: To declare the class name complex and define the function.Step3: And then read operation and calculate the values.Step4: Display the value by using the void display() function.Step5: Then to create a object and utilites the value.Step6: Stop the program.

  • //Concatenation of Two Strings#include#include#includeclass complex(){char *x;Public:complex(){}complex(char * real){x=real;}friend complex operator+(complex,complex);void display();};complex operator+(complex c,complex d){char *e;e=c.x;return(strcat(e,d.x));}void complex::display(){cout
  • }void main(){clrscr();complex c1(vijay);c1.display();complex c2(sangeetha);c2.display();complex c3=c1+c2;c3.display();getch();}

    Sample input and output:VijaySangeethaVijaysangeetha.

    Result:Thus the above program was successfully executed and the output was verified

    RE

  • 5. STATIC DATAMEMBERSAim:

    To write a c++ program to implement static data members.Algorithm:Step1: Start the program.Step2: To declare the data member function static data and count value.Step3: Declare the value get data these get count function.Step4: write value for member value a decrement the value count.Step5: The three values are declared in main functional a, b, c.Step6: The value is declared in values.Step7: The last return statement.Step8: Stop the program.

  • //Static Data Member#include#includeclass item{static int count;int number;public:void getdata(int a){number=a;count++;}void getcount(void){cout
  • c.getdata(300);c.getcount();cout
  • 6. STATIC MEMBER FUNCTIONAim:

    To write a c++ program to implement static member function.Algorithm:Step1: start the program.Step2: To declare the data member function static data and count value.Step3: Declare a value get data then get count function.Step4:Write a value for member value a decrement ,the value count it.Step5: Stop the program.

  • //Static Member Function#include#includeclass test{int code;static int count;public:void setcode(void){code=++count;}void showcode(void){cout
  • t1.setcode();t2.setcode();t3.setcode();test::showcount();t1.showcode();t2.showcode();t3.showcode();getch();terurn 0;}

    Sample Input And Output:***********************count:3objectno:1objectno:2objectno:3

    Result:Thus the above program was successfully executed and output was verified.

  • 7. STUDENTS DETAILS USING ARRAY OF OBJECTSAim:

    To write a c++ program to implement array on object to accept and display the studentdetails.Algorithm:Step1: Start the program.Step2: Object the classes there are scan of array object to student marks enter the integer values.Step3: Increase the value of array on object.Step4: The functional calls mark in student object.Step5: Stop the program.

  • //Student Details Using Array Objects

    #include#includeclass stud{private:char name[100];int m1,m2,m3,tot,no;float avg;public:void get(){cout

  • cout
  • Sample input and output:**********************Enter the no of student details u process: 3Enter the student no: 01Enter the student name: MayaEnter the three subject marks:788675Enter the student no: 02Enter the student name: ManiEnter the three subject marks:867596

    The student processed details are:No Student name Total Percentage01 Maya 239 79.6602 Mani 257 85.66

    Result:Thus the above c++ program was successfully executed and output was verified.

  • 8. IMPLEMENTATION OF COPY CONSTRUCTOR

    Aim:To implement the queries using SQL.

    Algorithm:Step1: Start the program.Step2: Object of the class then is the same of the copy constructor then the values.Step3: Declare the data member in private section.Step4: Declare the constructor add copy constructor in the private section.Step5: Create the object in the main function and of object as function argument using

    ConstructorStep6: Stop the program.

  • //Copy Constructor#include#includeclass e{private:int a;public:e(){}e(int w){a=w;}e(e&s){a=s.a;cout
  • void main(){clrscr();e e1(50);e e3(e1);cout
  • 9. IMPLEMENTATION OF FRIEND FUNCTION

    Aim:To write a c++ program to implement friend function.

    Algorithm:Step1: Start the program.Step2: Create the class declares and initialize to variable to friend in public section.Step3: In outside of the class reference as assurance and receive the friend function.Step4: Using the object reference to case the data member in class as return the processed

    values.Step5: In main function create the object and pass the object as function and can true series

    function.Step6: Stop the process.

  • //Friend Function#include#includeclass e{private:int a,b;public:void test(){a=100;b=200;}friend int compute (e e1);};int compute(e e1){return int(e1.a+e1.b);}void main(){clrscr();e s;s.test();cout
  • Sample input and output:************************THE RESULT IS: 300

    Result:Thus he above program was successfully executed and output was verified.

  • 10. IMPLEMENTATION OF RETURNING OBJECTAim:

    To write a c++ program to implement returning object.Algorithm:Step1: Start the program.Step2: Create a class declares the data member and member function and friend function.Step3: Input the values from the keyboard to the input function.Step4: Sum function is used to sum to complex values.Step5: Create the object to the main function display the sum of the two complex member the

    display functionStep6: Stop the program.

  • //Returning Object#include#includeclass complex{float x;float y;public:void input(float real,float imag){x=real;y=imag;}friend complex sum(complex,complex);void show(complex);};complex sum(complex c1,complex c2){complex c3;c3.x=c1.x+c2.x;c3.y=c1.y+c2.y;return(c3);}void complex::show (complex c){cout
  • int main(){clrscr();complex a,,b,c;a.input(3.1,5.65);b.input(2.75,1.2);c=sum(a,b);cout
  • 11. UNARY OPERATOR OVERLOADING

    Aim:To write a C++ program to implement Unary Operator Overloading.

    Algorithm:Step1: Start the programStep2: To create a class name un.Step3: To create a Function of a classStep4: To using the Unary Operator and Decrement Operator.Step5: To create the objects of the class.Step6: Display the values to call the function using objects.Step7: The last return the Statement.Step8: Stop the Program.

  • //Unary Operator Overloading#include#includeclass un{int x,y,z;public:void getdata(int a,int b,int c);void display(void);void operator--();};void un::getdata(int a,int b,int c){x=a;y=b;z=c;}void un display(void){cout
  • z=-z;}int main(){clrscr();un s;s.getdata(10,-20,30);cout
  • 12. DATA CONVERSIONAim:

    To write a C++ program using Data Conversion.Algorithm:Step1: Start the ProgramStep2: Create the class S.Step3: Declare the variable code.Step4: Declare the define the operator function in the public section.Step5: Create the Main FunctionStep6: Create the object d declare variable inStep7: Convert the objects and data the integer data.Step8: Stop the Program.

  • //Data Conversion#include#includeclass time{int h,m;public:time(){h=m=0;}time(int a,int b){h=a;m=b;}void show(){cout
  • };int main(){clrscr();time t1;int duration=189;t1=duration;cout
  • 13. POINTER TO OBJECTAim:

    To write a C++ Program to implement the Pointer to ObjectsAlgorithm:Step1: Start the programStep2: Create the class name S.Step3: Declare the class Variable i,n,r,s.Step4: Declare and define the member function is public section, get(), void cal(),

    void disp().

    Step5: Start the main program.Step6: Create the Objects with Pointer.Step7: Call the function S->get(), S->cal(), S->disp().Step8: Stop the Program.

  • //Pointer to Object#include#includeclass si{private:int p,n,r,si;public:void get(){coutp;coutn;coutr;}void cal(){si=(p*n*r)/100;}void disp(){cout
  • };void main(){clrscr();si*s;s->get();s->cal();s->disp();getch();}

    Sample Input and Output:Enter the Principle Value: 1500Enter the Number of Periods: 1Enter the Rate of Interest: 2

    Si=30

    Result:Thus the C++ Program was executed successfully and Output is verified.

  • 14. STUDENT DETAILS USING SINGLE INHERITANCEAim:

    To write a C++ program to implement Student Details Using Single Inheritance.Algorithm:Step1: Start the Program.Step2: Create the basic class name stud.Step3: Declare and define the function void get().Step4: Create the derived class name d stud in base class stud.Step5: Declare the variables marks, tot, avg.Step6: Declare the function void get marks(), void cal(), void display().Step7: Start the main program.Step8: Create the objects d to derived class d studStep9: Call the function.Step10: Stop the program.

  • //Single Inheritance#include#includeint stno;char sname[100];class stud{public:void get(){cout
  • cin>>m1>>m2>>m3>>m4>>m5;}void cal(){tot=m1+m2+m3+m4+m5;avg=tot/5;}void display(){cout
  • }Sample Input and Output

    ~~~~~~~~~~~~input~~~~~~~~~~~~Enter the Student No: 01Enter the Student Name: SudhaEnter the 5 Subject Mark: 98 89 78 98 88~~~~~~~~~~~~output~~~~~~~~~~~~The Student Name= SudhaThe Student No= 1Total= 451Avg= 90

    Result:Thus the C++ program was executed successfully and output is verified.

  • 15. VIRTUAL FUNCTIONAim:

    To write a C++ program to implement the Virtual Function.Algorithm:Step1: Start the Program.Step2: Create the class names.Step3: Declare the Virtual Function, using virtual keywordStep4: Create the define class d in base class s.Step5: Start the main program.Step6: Create objects in derived class and create the pointer objects in base class.Step7: Call the disp() function using & Pointer Object.Step8: Stop the Program.

  • //Virtual Function#include#includeclass s{public:virtual void disp(){cout
  • h->disp();h=&e;h->disp();getch();}

    Sample Input and OutputBase class display functionDerived class functionResult:

    Thus the C++ program was executed successfully and output is verified.

  • 16. VIRTUAL BASE CLASSAim:

    To write a C++ program to implement the Virtual Base Class.Algorithm:Step1: To start the program.Step2: Create the base class name a.Step3: Declare the member function.Step4: Create the Virtual derived class f from base class a.Step5: Create the derived class from class s and cStep6: Create the derived class from base class a.Step7: Start the main program.Step8: Create the objects for the derived class dStep9: Call the function using objectsStep10: Stop the program.

  • //Virtual Base Class#include#includeclass a{public:void disp(){cout
  • }};class d:public b,public c{public:void disp3(){cout
  • Result:Thus the C++ program was executed successfully and output is verified.

  • 17. THIS POINTERAim:

    To write a C++ program to implement the This Pointer.Algorithm:Step1: To start the programStep2: Create the class name person.Step3: Declare the variable new age.Step4: Create the function declare the pointer variable strcpy pointer variable.Step5: Return the This Pointer.Step6: Create the main program.Step7: Create the object c1,c2,c3 assign.Step8: Call the function.Step9: Stop the program.

  • //This Pointer#include#include#includeclass person{char name[20];float age;public:person(char *s,float a){strcpy(name,s);age=a;}person & person::greater(person &x){if(x.age>=age)return x;elsereturn *this;}void display(void){cout
  • };void main(){clrscr();person p1(John,37.50),p2(Ahamed,29.0),p3(Hebber,40.25);person p=p1.greater(p3);cout
  • Result:Thus the C++ program was executed successfully and output is verified.

  • 18. MULTIPLE INHERITANCEAim:

    To write a C++ program to implement a Multiple Inheritance.Algorithm:Step1: To start the programStep2: Declare a class name student having get() function which tabs input about

    Student no and name.

    Step3: Create another class student details which marks from user on input.Step4: Create another class output which inheritance student and student details

    class in

    which output functions define in which calculate total marks.

    Total=Math+Science+English

    Display all details.

    Step5: Stop the program.

  • //Multiple Inheritances#include#includeclass student{protected:long int sno;char sname[40];public:void get1(){coutsno;coutsname;}};class StudentDetails{protected:int math,science,english;public:void get2(){cout
  • cin>>math;coutscience;coutenglish;}};class output:public student,public StudentDetails{protected:float tot;float avg;public:void put(){tot=math+science+english;avg=tot/3;cout
  • void main(){output ob;clrscr();cout
  • Student Name: Sudhakar

    Total Marks: 283

    Average Mark: 94.333336

    Result:Thus the C++ program was executed successfully and output is verified.

  • 19. POINTER TO DERIVED CLASS OBJECTSAim:

    To write a C++ program to implement the Pointer to Derived Class Objects.Algorithm:Step1: To start the program.Step2: Create the class name s.Step3: Create the virtual function in public section.Step4: Declare the pointer object *b.Step5: Stop the program.

  • //Pointer to Derived Class Object#include#includeclass bc{public:int b;void show(){cout
  • bc base;bptr=&base;bptr->b=100;coutb=200;coutd=300;cout
  • Bptr Now Points to Derived ObjectB=200Dptr is Derived Type Pointer=200D=300Using((dc *)bptr)=200D=400

    Result:Thus the C++ program was executed successfully and output is verified.