+ All Categories
Home > Documents > Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print...

Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print...

Date post: 07-Jun-2018
Category:
Upload: trancong
View: 215 times
Download: 0 times
Share this document with a friend
41
Engineers Zone (www.engineerszone.com) 1 Exp.No: 1 IMPLEMENTATION OF CLASS AIM: A program to solve a quadratic equation, using OOP techniques. ALGORITHAM: 1) Start the process 2) Invoke the classes 3) Get the input for a,b,c; 4) Call the function getinfo() and display() 5) Check if a=0 a) True : compute c/b; i) Print the value of a/c; b) False: compute b*b-4*a*c; i) If ( b*b-4*a*c)=0 ii) Call img(); iii) Otherwise : call real(a,b,c); 6) Stop the process PROGRAM: #include<iostream.h> #include<conio.h> class equation { private:float a,b,c; public: void getinfo(float a, float b,float c ); void display( ); void equal(float a, float b); void imag( ); void real(float a,float b,float c); }; void equation :: getinfo(float aa,float bb,float cc) { a=aa; b=bb; c=cc; } void equation::display( ) { cout<<endl; cout<<”a=”<<a<<’\t’; cout<<”b=”<<b<<’\t’; cout<<”c=”<<c<<endl; }
Transcript
Page 1: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 1

Exp.No: 1

IMPLEMENTATION OF CLASS AIM: A program to solve a quadratic equation, using OOP techniques. ALGORITHAM:

1) Start the process 2) Invoke the classes 3) Get the input for a,b,c; 4) Call the function getinfo() and display() 5) Check if a=0

a) True : compute c/b; i) Print the value of a/c;

b) False: compute b*b-4*a*c; i) If ( b*b-4*a*c)=0 ii) Call img(); iii) Otherwise : call real(a,b,c);

6) Stop the process

PROGRAM: #include<iostream.h> #include<conio.h> class equation { private:float a,b,c; public: void getinfo(float a, float b,float c ); void display( ); void equal(float a, float b); void imag( ); void real(float a,float b,float c); }; void equation :: getinfo(float aa,float bb,float cc) { a=aa; b=bb; c=cc; } void equation::display( ) { cout<<endl; cout<<”a=”<<a<<’\t’; cout<<”b=”<<b<<’\t’; cout<<”c=”<<c<<endl; }

Page 2: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 2

void equation ::equal (float a,float b) { float x; x = -b/(2*a); cout<<”Roots are equal “<<x<<endl; } void equation :: imag( ) { cout<<”Roots are imaginary”; } void equation :: real(float a,float b,float det) { float x1,x2,temp; temp = sqrt(det); x1= (-b + temp)/(2*a); x2 = (-b –temp)/(2*a); cout<<”Roots are real”; cout<<”x1= “<<x1<<endl; cout<<”x2 =”<<x2<<endl; } void main( ) { class equation e; float aa,bb,cc; clrscr( ); cout<<”Enter the three numbers”; cin>>aa>>bb>>cc; e.getinfo(aa,bb,cc); e.display( ); if(aa = =0) { float temp; temp = cc/bb; cout<<” Linear Roots”<<temp<<endl; } else { float det; det = (bb*bb – 4*aa*cc); if(det = =0) e.equal(aa,bb); else if (det<0 ) e.imag( ); else e.real(aa,bb,cc ); } getch( ); }

Page 3: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 3

OUTPUT: Enter the three numbers 2 4 1 Roots are imaginary X1= - 0.292893 X2= - 1.707107 RESULT: Thus the program is executed and its output is verified.

Page 4: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 4

Exp.No: 2

CONSTRUCTOR AND DESTRUCTOR AIM: A program to print student details using constructor and destructor ALGORITHAM:

1. Start the process 2. Invoke the classes 3. Call the read() function

a. Get the inputs name ,roll number and address 4. Call the display() function

a. Display the name,roll number,and address of the student 5. Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class stu {

private: char name[20],add[20]; int roll,zip; public: stu ( );//Constructor ~stu( );//Destructor void read( ); void disp( );

}; stu :: stu( ) { cout<<”This is Student Details”<<endl; } void stu :: read( ) { cout<<”Enter the student Name”; cin>>name; cout<<”Enter the student roll no “; cin>>roll; cout<<”Enter the student address”; cin>>add; cout<<”Enter the Zipcode”; cin>>zip; } void stu :: disp( ) { cout<<”Student Name :”<<name<<endl;

cout<<”Roll no is :”<<roll<<endl;

Page 5: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 5

cout<<”Address is :”<<add<<endl; cout<<”Zipcode is :”<<zip; } stu : : ~stu( ) { cout<<”Student Detail is Closed”; } void main( ) { stu s; clrscr( );

s.read ( ); s.disp ( ); getch( );

}

Output: Enter the student Name

James Enter the student roll no 01 Enter the student address Newyork Enter the Zipcode 919108 Student Name : James Roll no is : 01 Address is : Newyork Zipcode is :919108

RESULT: Thus the program is executed and its output is verified.

Page 6: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 6

Exp.No: 3

FRIEND FUNCTION AIM:

A program to illustrate the use of dereferencing operators to access the class member

ALGORITHAM:

1) Start the process 2) Invoke the classes 3) Call the set_xy() first

a) Assign the value of x and y b) Print the value of x and y

4) Call the sum() for second(friend) a) Again assign the temp value of x and y

5) Print the value of x and y 6) Stop the process

PROGRAM

#include<iostream.h>

class M {

int x; int y; public: void set_xy(int a,int b) { x=a; y=b;

} friend int sum(M m); };

int sum(M m) {

int M ::* px=&M :: x; int M ::* py=&M :: y; M *pm=&m; int s=m.*px + pm->*py; return s;

}

Page 7: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 7

main() {

M n; void (M :: *pf)(int,int)=&M :: set_xy; (n.*pf)(10,20); cout<<"Sum="<<sum(n)<<"\n"; M *op=&n; (op->*pf)(30,40); cout<<"Sum="<<sum(n)<<"\n"; return(0);

} Output: Sum=30 Sum=70 RESULT: Thus the program is executed and its output is verified.

Page 8: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 8

Exp.No: 4 FUNCTION OVERLOADING

AIM:

A program to demonstrate how function overloading is carried out for swapping of two variables of the various data types, namely integer, floating point number and character types

ALGORITHAM:

• Start the process • Get the integer values of ix,iy • Get the floating values of fx,fy • Get the character values of cx,cy • Call swap(ix,iy)

o Assign temp<-a o Assighn a<-b,b<-temp

• Swapping the integer values • Print the value of ix and iy • Swapping floating values • Print the values oh fx and fy • Swapping on characters • Print the value of cx,cy • Stop the process

PROGRAM #include<iostream.h> #include<conio.h>

void swap(int &ix,int &iy); void swap(float &fx,float &fy); void swap(char &cx,char &cy);

void main() {

int ix,iy; float fx,fy; char cx,cy;

clrscr(); cout<<"Enter 2 integers:"; cin>>ix>>iy; cout<<"Enter 2 floating point no:s:"; cin>>fx>>fy; cout<<"Enter 2 characters:"; cin>>cx>>cy; cout<<"\nIntegers:"; cout<<"\nix="<<ix<<"\niy="<<iy; swap(ix,iy); cout<<"\nAfter swapping";

cout<<"\nix="<<ix<<"\niy="<<iy;

Page 9: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 9

cout<<"\nFloating point no:s"; cout<<"\nfx="<<fx<<"\nfy="<<fy; swap(fx,fy); cout<<"\nAfter swapping"; cout<<"\nfx="<<fx<<"\nfy="<<fy; cout<<"\nCharacters"; cout<<"\ncx="<<cx<<"\ncy="<<cy; swap(cx,cy); cout<<"\nAfter swapping"; cout<<"\ncx="<<cx<<"\ncy="<<cy;

getch(); }

void swap(int &a,int &b) { int temp; temp=a; a=b; b=temp; }

void swap(float &a, float &b) {

float temp; temp=a; a=b; b=temp; }

void swap(char &a, char &b) { char temp; temp=a; a=b; b=temp; }

Output: Enter 2 integers: 100 200 Enter 2 floating point no:s :-11.11 22.22 Enter 2 characters: s t

Integers: Ix=100 Iy=200 After swapping Ix=200 Iy=100 Floating point no: Fx=-11.11 Fy=22.22

After swapping

Page 10: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 10

Fx=22.22 Fy=-11.11 Characters Cx=s Cy=t After swapping Cx=t Cx=s

RESULT: Thus the program is executed and its output is verified.

Page 11: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 11

Exp.No .5

UNARY OPERATOR

AIM: A program for overloading the unary operator ++.

ALGORITHAM: • Start the process • Invoke the class counter • Crate two objects c1 and c2 • Assign values to c1 an c2

o Call c1.get_count() o Call c2.get_count()

• Increment the values o C1++ o C2++ o ++c2

• Print c1 and c2 • Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class counter {

int count; public: counter() {

count=0; }

int get_count() {

return count; } void operator++() {

count++; }

};

void main() {

counter c1,c2; cout<<"\nC1 ="<<c1.get_count(); cout<<"\nC2 ="<<c2.get_count();

c1++; //Using overloaded ++ operator.

c2++;

Page 12: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 12

++c2; cout<<"\nC1 ="<<c1.get_count(); cout<<"\nC2 ="<<c2.get_count();

getch(); }

OUT PUT: C1=0 C2=O C1=1 C2=2 RESULT: Thus the program is executed and its output is verified.

Page 13: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 13

Exp.No .6

BINARY OPERATOR

AIM: A program to perform simple arithmetic operations of two complex numbers using operator overloading.

ALGORITHAM:

• Start the process • Get the complex value a.real and a.image • Check while ((ch=getchar())!=’q’)

o True : execute switch(ch) o Case ‘a’:Then

Compute c<-a+b, Print c.real and c.imag o Case ‘s’: Then

Compute c<-a-b, Print c.real and c.imag o Case ‘m’: Then

Compute c<-a*b, Print c.real and c.imag o Case ‘d’: Then

Compute c<-a/b, Print c.real and c.imag o End of switch

• End of while • Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> #include<string.h> struct complex {

float real; float imag;

}; complex operator + (complex a,complex b); complex operator - (complex a,complex b); complex operator * (complex a,complex b); complex operator / (complex a,complex b);

void main() {

complex a,b,c; int ch; void menu(void);clrscr(); cout<<"Enter the first complex no:"; cin>>a.real>>a.imag;

cout<<"Enter the second complex no:";

Page 14: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 14

cin>>b.real>>b.imag; menu(); while ((ch = getchar()) != 'q') {

switch(ch) {

case 'a':c =a + b; cout<<"Addition of 2 no’s"; cout<<c.real<<"+i"<<c.imag; break; case 's':c=a-b; cout<<"Substraction of 2 no’s"; cout<<c.real<<"i"<<c.imag; break; case 'm':c=a*b; cout<<"Multiplication of 2 no’s"; cout<<c.real<<"i"<<c.imag; break;

case 'd':c=a/b; cout<<"Division of 2 no’s"; cout<<c.real<<"i"<<c.imag; break;

} } } void menu()

{ cout<<"complex no: operators"; cout<<"a->addition"; cout<<"s->substraction"; cout<<"m->multiplication"; cout<<"d->division"; cout<<"q->quit"; cout<<"options please";

} complex operator -(struct complex a, struct complex b) {

complex c; c.real=a.real-b.real; c.imag=a.imag-b.imag; return(c);

} complex operator *(struct complex a, struct complex b) {

complex c; c.real=((a.real*b.real)-(a.imag*b.imag)); c.imag=((a.real*b.imag)+(a.imag*b.real)); return(c);

}

complex operator +(struct complex a,struct complex b)

Page 15: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 15

{ complex c; c.real=a.real+b.real; c.imag=a.imag+b.imag; return(c);

} complex operator /(struct complex a, struct complex b) {

complex c; float temp; temp=((b.real*b.real)+(b.imag*b.imag)); c.real=((a.real*b.real)+(a.imag*b.imag))/temp;

return(c); }

OUTPUT Enter the first complex no: 1,1 Enter the second complex no: 2,2 Addition of 2 no’s : 3+I3

RESULT: Thus the program is executed and its output is verified.

Page 16: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 16

Exp.No .7

SINGLE INHERITANCE AIM:

A program to illustrate a single inheritance. We have a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions

ALGORITHAM:

• Start the process • Invoke the base class B • Invoke the derived class D using public derivation • Get the input data • Display the inputted data • Call the derived classes member functions

o Assign a new value for base classes data member • Display the outputs • Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class B {

int a; public: int b; void get_ab(); int get_a(); void show_a();

}; class D: private B {

int c; public: void mul(); void display();

}; void B::get_ab() {

cout<<"Enter Values for a and b"; cin>>a>>b;

} int B::get_a() {

return a;

Page 17: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 17

} void B::show_a(){

cout<<"a= "<<a<<"\n"; }

void D::mul() {

get_ab(); c=b*get_a(); }

void D:: display() {

show_a(); cout<<"b= "<<b<<"\n"; cout<<"c= "<<c<<"\n\n";

} void main() {

clrscr(); D d; d.mul(); d.display(); d.mul(); d.display(); getch(); } OUTPUT A=5

A=5 B=10 C=50 A=5 B=20 C=100

RESULT: Thus the program is executed and its output is verified.

Page 18: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 18

Exp.No .8

MULTILEVEL INHERITANCE AIM:

A program to illustrate multilevel inheritance. we have three classes, student, test and result. Here class student is the base class. And the class test is derived from the base class student. And the another class result is derived from the class test.

ALGORITHAM:

• Start the process • Invoke the base class student • Invoke the derived class test which in inherited by the class student • Invoke the derived class result which in inherited by the class test • Create an object student1 for the result class • Call student1.getno(),assign the value of rno in student class • Call student1.getmarks(),assign the marks in test class • Call student1.display(),for displaying the result • Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class student {

protected: int rno; public: void getno(int); void putno(void);

}; void student::getno(int a) {

rno=a; } void student ::putno() {

cout<<"rollno="<<rno<<endl; } class test:public student {

protected: float sub1; float sub2; public:

void getmarks(float,float);

void putmarks(void);

Page 19: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 19

}; void test::getmarks(float x,float y)

{ sub1=x; sub2=y;

} void test::putmarks()

{ cout<<"marks in sub1="<<sub1<<endl; cout<<"marks in sub2="<<sub2<<endl;

} class result:public test {

float total; public: void display(void);

}; void result::display(void) {

total=sub1+sub2; putno(); putmarks(); cout<<"total="<<total<<endl;

} int main() { clrscr(); result student1; student1.getno(111); student1.getmarks(75.0,59.5); student1.display(); getch(); return 0; getch();

} OUTPUT Roll number:111 Marks in sub1=75 Marks in sub2=59.5 Total=134.5 RESULT: Thus the program is executed and its output is verified.

Page 20: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 20

Exp.No .9

MULTIPLE INHERITANCES

AIM: ` Write a program to illustrating how all the three classes are implemented in multiple inheritance mode ALGORITHM

• Start the process • Invoke the class M • Invoke the another class N • Invoke one more class,ie class P,which is inherited by both M and N • Create an object for class p,ie P p • Call p.get_m(),assign the value in to ‘m’ • Call p.get_n(),assign the value in to ‘n’ • Call display(), for dispay the result • Stop the result

PROGRAM: #include<iosteram.h> #include<conio.h> Class M

{ Protected: Int m; Public : Void get_M(); }; Class N

{ Protected: Int n; Public: Void get_N(); }; Class p: public M, public N { Public: Void disply(void); }; Void M ::get_m(int x) { m=x;

}

Page 21: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 21

Void N::get_n(int y) { n=y; } Void P:: disply(void) { Cout<<”m=”<<m<<endl; Cout<<”n=”<<n<<endl; Cout<<”m*n=”<<m*n<<endl; } int main() { P p; p.get_m(10); p.get_n(20); p.display(); return 0; } OUTPUT m=10 n=20 m*n=200 RESULT: Thus the program is executed and its output is verified.

Page 22: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 22

Exp.No .10

HYBRID INHERITANCE #include<iostream.h> #include<conio.h> class stu {

protected: int rno;

public: void get_no(int a)

{ rno=a;

} void put_no(void)

{ out<<"Roll no"<<rno<<"\n"; } }; class test:public stu { protected: float part1,part2;

public: void get_mark(float x,float y) { part1=x; part2=y; } void put_marks() { cout<<"Marks obtained:"<<"part1="<<part1<<"\n"<<"part2="<<part2<<"\n"; } }; class sports { protected: float score;

public: void getscore(float s) { score=s; } void putscore(void) { cout<<"sports:"<<score<<"\n"; }

};

Page 23: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 23

class result: public test, public sports { float total;

public: void display(void); }; void result::display(void) { total=part1+part2+score; put_no(); put_marks(); putscore(); cout<<"Total Score="<<total<<"\n"; } int main() { clrscr(); result stu; stu.get_no(123); stu.get_mark(27.5,33.0); stu.getscore(6.0); stu.display(); return 0; }

OUTPUT Roll no 123 Marks obtained : part1=27.5 Part2=33 Sports=6 Total score = 66.5 RESULT: Thus the program is executed and its output is verified.

Page 24: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 24

Exp.No .11

STATIC MEMBER FUNCTIONS AIM

Write a program to illustrate the static member function. ALGORITHM

1. Start the process 2. Invoke the class

i. Set the data member and member function as a static b. Create two objects t1 and t2

3. Call the function t1.setcode i. Increment the value of data member count

4. Call the function t1.setcode i. Increment the value of data member count

5. Call the static member function showcount() i. Display the value of count

6. Create a new object t3 7. Call the function t3.set code() 8. Call t1.showcount(), t2.showcount() and t3.showcount() 9. Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class test { int code; static int count; public : void setcode(void) { code= ++count; } void showcode(void) { cout<<"object number"<<code<<endl; } static void showcount(void) { cout<<"count"<<count<<endl; } }; int test::count;

Page 25: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 25

int main() { clrscr(); test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode();

test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); return(1); }

OUTPUT Count 2 Count 3 Object number 1 Object number 2 Object number 3 RESULT: Thus the program is executed and its output is verified.

Page 26: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 26

Exp.No .12

VIRTUAL FUNCTIONS AIM A program to access the member of the derived class objects through an array of members. In this program, both the base class and the derived class member functions are preceded by the keyword ALGORITHM

1. Start the process 2. Invoke the class with pointer 3. Assign ptr[0] <- &objb;

i. Assign ptr[1] <- &objc; 4. Ptr(0)points the getdata()

a. Get the values of x and y 5. Ptr(1) points to the getdata()

a. Get the roll no and name of the student 6. Ptr(0) and ptr(1) are points to the display()

a. Print the values 7. Stop the process

PROGRAM

#include<iostream.h> #include<conio.h>

class base { private: int x; float y; public: virtual void getdata( ); virtual void display( ); }; class devb: public base { private: int roll; char name[20]; public: virtual void getdata( ); virtual void display( ); }; class devc : public base { private: float height; float weight; public : virtual void getdata( ); virtual void display( ); }:

void base :: getdata( )

Page 27: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 27

{ cout<<” Enter any Integer”; cin>>x; cout<<”Enter a real no”; cin>>y; } void base ::display( ) { cout<<”The no X=”<<x<<”Y=”<<y<<endl; } void devb :: display( ) { cout<<”Roll of the Student is:”<<roll<<endl; cout<<” Name of the Student is: “<<name<<endl; } void devb :: getdata( ) { cout<<”Enter the Roll of the Student:”; cin>>roll; cout<<”Enter Name of Student :”; cin>>name; } void devc :: getdata( ) { cout<<”Enter height and weight”; cin>>height>>weight; } void devc :: display( ) { cout<<”Height :”<<height<<endl; cout<<”Weight :”<<weight<<endl; } void main( ) { base *ptr[3]; devb objb; devc objc; clrscr( ); ptr[0] = &objb; ptr[1] = &objc; ptr[0] ->getdata( ); ptr[1] -> getdata( ); ptr[1] -> display( ); ptr[1] -> display( ); getch ( ); }

Page 28: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 28

OUTPUT

Enter the Roll of the Student: 101 Enter Name of Student : salah height and weight 170 72 Roll of the Student is:101 Name of the Student is:salah Height :170 Weight :72

RESULT: Thus the program is executed and its output is verified.

Page 29: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 29

Exp.No .13

PURE VIRTUAL FUNCTION AIM A program to demonstrate how a pure virtual function is defined, declared and invoked from the object of derived class through the pointer of the base class. ALGORITHM

1. Start the process 2. Invoke the class with pointer 3. Assign ptr<-&obj 4. Call the ptr->getdata()

a. Get the roll no and name of the student 5. Call the ptr->display()

a. Print the roll no and name 6. Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> class base { private: int x; float y; public : virtual void getdata( ); virtual void display( ); }; class dev : public base { private: int roll; char name[20]; public : void getdata( ); void display( ); }; void base :: getdata( ) { } void base :: display( ) { } void dev :: getdata( ) { cout<<” Enter Roll of the Student “; cin>> roll; cout<<” Enter name of the student”; cin>>name;

} void dev :: display( ) { cout<<”Name is :”<<name<<endl;

cout<<” Roll no is :”<<roll <<endl;

Page 30: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 30

} void main( ) { base * ptr; dev obj; clrscr( ); ptr = &obj; ptr -> getdata( ); ptr -> display( ); getch( ); } OUTPUT

Enter the roll no of the student: 111 Enter the name of the student : Kapil Dev Name is : Kapil Dev Roll no is : 111

RESULT: Thus the program is executed and its output is verified.

Page 31: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 31

Exp.No .14 FILE

AIM Write a program to illustrate the write() member function which are usually used for transfer of data blocks to the file ALGORITHM

1. Start the process 2. Invoke the class

a. Create two inline member functions .ie, getdata() and dispdata() i. getdata() for input

ii. dispdata() for display 3. Open the file in fstream mode 4. Write the data in to the file 5. Slose the file 6. Stop the process

PROGRAM

#include<iostream.h> #include<conio.h> #include<fstream.h> class student {

private: int rno; char name[10]; float fees;

public: void getdata() {

cout<<"roll number"; cin>>rno; cout<<endl; cout<<"enter name:"; cin>>name; cout<<endl<<"enter fees:"; cin>>fees;

} void dispdata() {

cout<<"Roll number"<<rno<<endl; cout<<"Name"<<name<<endl; cout<<"Fees"<<fees;

} };

Page 32: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 32

void main() {

student s1; clrscr(); ofstream stdfile("c:\\std.txt"); //fstream stdfile; //stdfile.open("c:\\std.txt",ios::out|ios::in); //open file for output char wish; //writing to the file do {

s1.getdata(); stdfile.write((char*)&s1,sizeof(student)); cout<<"continue ? y/n"; cin>>wish;

} while(wish=='y'||wish=='Y'); stdfile.close(); //close the file

getch();

}

OUTPUT Roll number 121 Enter name Jacob Enter fees 10000

RESULT: Thus the program is executed and its output is verified.

Page 33: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 33

Exp.No .15

COPYING ONE FILE TO ANOTHER FILE AIM A program to copy one file to another file and convert the lower case characters to upper case characters. ALGORITHM

1) Start the process 2) Create the input file and out put file. 3) Get the input file name to fname1. 4) Get the output file name to fname2. 5) Open the infile(fanme1) 6) Check if infile.fail()

a) True: i) Execute error conversion ii) exit

7) open the outfile(fanme2) 8) check if outfile.fail()

a) repeat step(6:a) 9) check while infile.eof()

a) True: i) Ch (char)infile.get()

10) Close both files 11) Stop the process.

PROGRAM :

#include<iostream.h> #include<conio.h> #include<iomanip.h> #include<stdlib.h> #include<ctype.h> #include<fstream.h> void main( ) { ofstream outfile; ifstream infile; char fname1[10],fname2[20]; char ch,uch; clrscr( ); cout<<"Enter a file name to be copied "; cin>> fname1; cout<<"Enter new file name"; cin>>fname2; infile.open(fname1);

Page 34: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 34

if( infile.fail( ) )

{ cerr<< " No such a file Exit"; getch(); exit(1); } outfile.open( fname2); if(outfile.fail( )) { cerr<<"Unable to create a file"; getch(); exit(1); } while( !infile.eof( ) ) { ch = (char) infile.get( ); uch = toupper(ch); outfile.put(uch); } infile.close( ); outfile.close( ); getch( ); }

OUTPUT: Enter a file name to be copied. C:\text1.txt Enter new file name D:\new.txt Input file Asbcdefghijklmnopqrstuvwxyz Output file ASBCDEFGHIJKLMNOPQRSTUVWXYZ RESULT: Thus the program is executed and its output is verified.

Page 35: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 35

Ex.no: 1

CREATION OF TABLE Creates a table and define its column and other properties. Also contains pending changes to the database. Syntax: CREATE TABLE <TABLE-NAME>(COLUMN SPEC[NULL|NOT NULL],..); Here Spec - Specifies the column’s type and size. Null - specifies at the fields in this column may be null. Not Null- Specifies that may not be null. Examples: SQL> CREATE TABLE EMP(EMP_NO NUMBER(3) NOT NULL, ENAME VARCHAR2(20),JOB VARCAHR2(10), HIRE_DATE DATE, SALARY NUMBER(6,2); Table created

DESCRIBE A TABLE Display a brief description of the specified table owned by the specified uses if user is omitted, SQL assumes you own the table. Syntax: DESC <TABLE NAME> Example: Desc EMP

Name NULL? TYPE EMP_NO NOT NULL NUMBER ENAME NULL VARCHAR2 JOB NULL VARCHAR2 HIRE_DATE NULL DATE SALARY NULL NUMBER

Page 36: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 36

INSERTION OF TABLE

The insert command inserts one or more rows into a table Syntax:

INSERT INTO <TABLE NAME>(COLUMN NAME,…………..) VALUES(VALUE1, VALUE2……..);

OR

INSERT INTO <TABLE NAME>VALUES(&COULMN NAME, &COULMN NAME…);

Examples:

INSERT INTO EMP VALUES(122,’XYZABC’,’CLERK’,’11-JUL - 2006’,5000.00);

ALTERATION OF TABLES

This command is specifies the alteration of the tables. Syntax:

ALTER TABLE <TABLE NAME>[ADD/MODIFY](COLUMN DATA TYPE(SIZE)….);

Examples: ALTER TABLE EMP ADD(ADDRESS VARCHAR2(30));

ALTER TABLE EMP MODIFY(ENAME VARCHAR2(10),CITY VARCHAR(10));

UPDATION OF TABLES

The UPDATE command of an UPDATE clause followed by a set clause and an WHERE clause.

Syntax: UPDATE<TABLE NAME> SET COLUMN-NAME=EXPR [WHERE CONDITION];

Examples: UPDATE EMP SET SALARY=3000 WHERE JOB=’CLERAK’;

Page 37: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 37

DELETION OF A TABLE

The delete command is used to delete the contents of the fields in a given table. Syntax: DELETE FROM <TABLE-NAME> DELETE FROM <TABLE> WHERE CONDITION Example: DELETE FROM EMP; DELETE FROM EMP WHERE EMP_NO=11;

DROPING A TABLE

The drop command is used to drop the entire function or table Syntax: DROP TABLE <TABLE NAME> Examples: DROP TABLE EMP;

Page 38: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 38

DATA CONTROL

Ex.no: 2

GRANT : Provides various types of access to database objects, such as tables,views, sequences. Syntax:

GRANT {OBJECT-PRIVILEGE}\{ALL}ON[USER] OBJECT TO{USER\PUBLIC} [WITH GRANT OPTION]

Example: GRANT ALL ON EMP TO SMITH WITH GRANT OPTION;

REVOKE

Revoke privileges from one or more users for tables, views and sequences. Syntax:

REVOKE{ OBJECT-PRIVILAGE }\ALL ON USER OBJECT FROM [USER\PUBLIC]

Page 39: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 39

CONTROL STRUCTURE

In addition to SQL commands, PL/SQL can also process data using flow of control statements. The flow of control statements can be classified under the following categories:

• Conditional Control • Iterative Control • Sequential control

CONDITIONAL CONTROL

Syntax: IF CONDITION THEN SEQUENCES OF STATEMENTS; END IF;

ITREATIVE CONTROL Syntax:

• Simple loop • While loop • For loop

SIMPLE LOOP

Syntax: LOOP SEQUENCE OF STATEMENTS’ END LOOP; Program: DECLARE A NUMBER:=100; BEGIN LOOP A:=A+25; EXIT WHEN A=250; END LOOP;

Page 40: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 40

WHILE LOOP

Syntax: WHILE <CONDITION> LOOP SEQUENCE OF STATEMENTS; END LOOP; Program: DECLARE I NUMBER:=0; J NUMBER=0; BEGIN WHILE I<=100; LOOP J:=J+1; I:=I+5; END LOOP; DBMS_OUTPUT.PUT_LINE(‘The value of J=’||J); END

FOR LOOP Syntax: FOR COUNTER IN [REVERSE]LOWER BOUND…..UPPER BOUND LOOP SEQUENCES OF STATEMENTS; END LOOP; Program: BEGIN FOR I IN 1..2 LOOP

UPDATE EMP SET SALARY=SALARY+100 WHERE SALARY>3000. END LOOP END;

Page 41: Exp.No: 1 IMPLEMENTATION OF CLASS AIM: … · Exp.No: 1 IMPLEMENTATION OF CLASS AIM: ... print student details using constructor and ... to perform simple arithmetic operations of

Engineers Zone (www.engineerszone.com) 41

PL/SQL Ex.No.6 SQL> desc std_details Name Null? Type ------------------------------- -------- ---- STD_ID NOT NULL NUMBER(10) STD_NAME NOT NULL VARCHAR2(20) STD_MARK NOT NULL NUMBER(5) STD_REMARK NOT NULL VARCHAR2(10) SQL>ED std_inf; SET SERVEROUTPUT ON prompt Enter id of the Student accept s declare ID STD_DETAILS.STD_ID%TYPE; NAME STD_DETAILS.STD_NAME%TYPE; MARK STD_DETAILS.STD_MARK%TYPE; REM STD_DETAILS.STD_REMARK%TYPE; BEGIN SELECT STD_ID,STD_NAME,STD_MARK,STD_REMARK INTO ID,NAME,MARK,REM FROM STD_DETAILS WHERE STD_ID=&S; DBMS_OUTPUT.PUT_LINE('iD :'||ID); DBMS_OUTPUT.PUT_LINE('NAME :'||NAME); DBMS_OUTPUT.PUT_LINE('MARK :'||MARK); DBMS_OUTPUT.PUT_LINE('REMARK :'||REM); END; / SET SERVEROUTPUT OFF OUTPUT: SQL> @ std_inf; Enter id of the Student iD :1 NAME : Rahul Dravid MARK :100 REMARK :PASS


Recommended