C++ Source code for Different C++ and OOPS Concepts

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

#include

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<

cout<<”a=”<<<’\t’;

cout<<”b=”<<<’\t’;

cout<<”c=”<<

}

void equation ::equal (float a,float b)

{

float x;

x = -b/(2*a);

cout<<”Roots are equal “<<

}

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= “<<

cout<<”x2 =”<<

}

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”<<

}

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( );

OUTPUT:

Enter the three numbers 2 4 1

Roots are imaginary

X1= - 0.292893

X2= - 1.707107

RESULT:


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

#include

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”<

}

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 :”<<

cout<<”Roll no is :”<<

cout<<”Address is :”<<

cout<<”Zipcode is :”<

}

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.

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

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;

}

main()

{

M n;

void (M :: *pf)(int,int)=&M :: set_xy;

(n.*pf)(10,20);

cout<<"Sum="<<<"\n";

M *op=&n;

(op->*pf)(30,40);

cout<<"Sum="<<<"\n";

return(0);

}

Output:

Sum=30

Sum=70

RESULT:

Thus the program is executed and its output is verified.


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

#include

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="<<<"\niy="<

swap(ix,iy);

cout<<"\nAfter swapping";

cout<<"\nix="<<<"\niy="<

cout<<"\nFloating point no:s";

cout<<"\nfx="<<<"\nfy="<

swap(fx,fy);

cout<<"\nAfter swapping";

cout<<"\nfx="<<<"\nfy="<

cout<<"\nCharacters";

cout<<"\ncx="<<<"\ncy="<

swap(cx,cy);

cout<<"\nAfter swapping";

cout<<"\ncx="<<<"\ncy="<

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

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.



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

#include

class counter

{

int count;

public:

counter()

{

count=0;

}

int get_count()

{

return count;

}

void operator++()

{

count++;

}

};

void main()

{

counter c1,c2;

cout<<"\nC1 ="<

cout<<"\nC2 ="<

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

c2++;

++c2;

cout<<"\nC1 ="<

cout<<"\nC2 ="<

getch();

}

OUT PUT:

C1=0 C2=O

C1=1 C2=2

RESULT:

Thus the program is executed and its output is verified.


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

#include

#include

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:";

cin>>b.real>>b.imag;

menu();

while ((ch = getchar()) != 'q')

{

switch(ch)

{

case 'a':c =a + b;

cout<<"Addition of 2 no’s";

cout<<<"+i"<

break;

case 's':c=a-b;

cout<<"Substraction of 2 no’s";

cout<<<"i"<

break;

case 'm':c=a*b;

cout<<"Multiplication of 2 no’s";

cout<<<"i"<

break;

case 'd':c=a/b;

cout<<"Division of 2 no’s";

cout<<<"i"<

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)

{

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.


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

#include

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;

}

void B::show_a(){

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

}

void D::mul()

{

get_ab();

c=b*get_a();

}

void D:: display()

{

show_a();

cout<<"b= "<<<"\n";

cout<<"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.


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

#include

class student

{

protected:

int rno;

public:

void getno(int);

void putno(void);

};

void student::getno(int a)

{

rno=a;

}

void student ::putno()

{

cout<<"rollno="<<

}

class test:public student

{

protected:

float sub1;

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="<<

cout<<"marks in sub2="<<

}

class result:public test

{

float total;

public:

void display(void);

};

void result::display(void)

{

total=sub1+sub2;

putno();

putmarks();

cout<<"total="<<

}

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.


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

#include

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;

}

Void N::get_n(int y)

{

n=y;

}

Void P:: disply(void)

{

Cout<<”m=”<<

Cout<<”n=”<<

Cout<<”m*n=”<<

}

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.



Exp.No .10

HYBRID INHERITANCE

#include

#include

class stu

{

protected:

int rno;

public:

void get_no(int a)

{

rno=a;

}

void put_no(void)

{

out<<"Roll no"<<<"\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="<<<"\n"<<"part2="<<<"\n";

}

};

class sports

{

protected:

float score;

public:

void getscore(float s)

{

score=s;

}

void putscore(void)

{

cout<<"sports:"<<<"\n";

}

};

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="<<<"\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.


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

#include

class test

{

int code;

static int count;

public :

void setcode(void)

{

code= ++count;

}

void showcode(void)

{

cout<<"object number"<<

}

static void showcount(void)

{

cout<<"count"<<

}

};

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:

Thus the program is executed and its output is verified.


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

#include

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( )

{

cout<<” Enter any Integer”;

cin>>x;

cout<<”Enter a real no”;

cin>>y;

}

void base ::display( )

{

cout<<”The no X=”<<<”y=”<<

}

void devb :: display( )

{

cout<<”Roll of the Student is:”<<

cout<<” Name of the Student is: “<<

}

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 :”<<

cout<<”Weight :”<<

}

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 ( );

}

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.

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

#include

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 :”<<

cout<<” Roll no is :”<

}

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.

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

#include

#include

class student

{

private:

int rno;

char name[10];

float fees;

public:

void getdata()

{

cout<<"roll number";

cin>>rno;

cout<

cout<<"enter name:";

cin>>name;

cout<<<"enter>

cin>>fees;

}

void dispdata()

{

cout<<"Roll number"<<

cout<<"Name"<<

cout<<"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

Roll number 121

Enter name Jacob

Enter fees 10000

RESULT:

Thus the program is executed and its output is verified.

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

#include

#include

#include

#include

#include

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);

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.

1 comment:

Yuriy said...

If you are a candidate, and your interviewer asks you these questions - LEAVE IMMEDIATELY. You don't want to have a C++ freak as your boss or co-worker.