Académique Documents
Professionnel Documents
Culture Documents
Exp.No: 1
IMPLEMENTATION OF CLASS
AIM:
ALGORITHAM:
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;
}
2
OUTPUT:
Enter the three numbers 2 4 1
Roots are imaginary
X1= - 0.292893
X2= - 1.707107
RESULT:
Exp.No: 2
AIM:
A program to print student details using constructor and destructor
ALGORITHAM:
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;
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:
RESULT:
Exp.No: 3
FRIEND FUNCTION
AIM:
A program to illustrate the use of dereferencing operators to access the class
member
ALGORITHAM:
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;
}
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:
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:
#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";
9
cout<<"\nix="<<ix<<"\niy="<<iy;
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:
Integers:
Ix=100
Iy=200
After swapping
Ix=200
Iy=100
Floating point no:
Fx=-11.11
Fy=22.22
10
After swapping
Fx=22.22
Fy=-11.11
Characters
Cx=s
Cy=t
After swapping
Cx=t
Cx=s
RESULT:
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();
12
OUT PUT:
C1=0 C2=O
C1=1 C2=2
RESULT:
Exp.No .6
BINARY OPERATOR
AIM:
A program to perform simple arithmetic operations of two complex numbers
using operator overloading.
ALGORITHAM:
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();
14
return(c);
}
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;
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
RESULT:
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:
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;
17
}
int B::get_a()
{
return a;
}
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:
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:
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;
19
float sub2;
public:
void getmarks(float,float);
void putmarks(void);
};
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:
Exp.No .9
MULTIPLE INHERITANCES
AIM:
` Write a program to illustrating how all the three classes are implemented in
multiple inheritance mode
ALGORITHM
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);
21
};
Void M ::get_m(int x)
{
m=x;
}
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:
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)
23
{
cout<<"sports:"<<score<<"\n";
}
};
OUTPUT
Roll no 123
Marks obtained : part1=27.5
Part2=33
Sports=6
Total score = 66.5
RESULT:
Exp.No .11
STATIC MEMBER FUNCTIONS
AIM
Write a program to illustrate the static member function.
ALGORITHM
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;
}
25
};
int test::count;
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:
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
#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;
27
float weight;
public : virtual void getdata( );
virtual void display( );
}:
void base :: getdata( )
{
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 ( );
28
OUTPUT
RESULT:
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 :: display( )
{
cout<<”Name is :”<<name<<endl;
cout<<” Roll no is :”<<roll <<endl;
}
void main( )
{
base * ptr;
dev obj;
clrscr( );
ptr = &obj;
ptr -> getdata( );
ptr -> display( );
getch( );
}
OUTPUT
RESULT:
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
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;
32
cout<<"Name"<<name<<endl;
cout<<"Fees"<<fees;
}
};
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
RESULT:
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
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 ";
34
cin>> fname1;
cout<<"Enter new file name";
cin>>fname2;
infile.open(fname1);
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:
Input file
Asbcdefghijklmnopqrstuvwxyz
Output file
ASBCDEFGHIJKLMNOPQRSTUVWXYZ
RESULT:
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
INSERTION OF 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
Syntax:
ALTER TABLE <TABLE NAME>[ADD/MODIFY](COLUMN DATA
TYPE(SIZE)….);
Examples:
UPDATION OF TABLES
Examples:
UPDATE EMP SET SALARY=3000 WHERE JOB=’CLERAK’;
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:
DROPING A TABLE
Examples:
DATA CONTROL
Ex.no: 2
GRANT :
Syntax:
Example:
REVOKE
Revoke privileges from one or more users for tables, views and sequences.
Syntax:
REVOKE{ OBJECT-PRIVILAGE }\ALL ON USER OBJECT FROM
[USER\PUBLIC]
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
40
A:=A+25;
EXIT WHEN A=250;
END LOOP;
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:
END LOOP;
Program:
BEGIN
FOR I IN 1..2
LOOP
UPDATE EMP SET SALARY=SALARY+100 WHERE
SALARY>3000.
41
END LOOP
END;
PL/SQL
Ex.No.6
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
42
REMARK :PASS