0% found this document useful (0 votes)
69 views72 pages

Fycs Oops Manual

The document contains multiple C++ programs demonstrating various programming concepts including data members, member functions, branching, looping, constructors, destructors, and inheritance. Each section provides a specific problem statement followed by the corresponding C++ code and expected output. Key topics include arithmetic operations, area calculations, matrix operations, and scope resolution operator usage.

Uploaded by

xovowe6238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views72 pages

Fycs Oops Manual

The document contains multiple C++ programs demonstrating various programming concepts including data members, member functions, branching, looping, constructors, destructors, and inheritance. Each section provides a specific problem statement followed by the corresponding C++ code and expected output. Key topics include arithmetic operations, area calculations, matrix operations, and scope resolution operator usage.

Uploaded by

xovowe6238
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 72

1)Program to demonstrate the use of data members and member functions in C++

1a)Write a c++ program to add two numbers using function

Program:

#include<iostream.h>

#include<conio.h>

Void main()

clrscr();

Int n1,n2,sum;

cout<<”Enter two numbers\n”;

cin>>n1>>n2;

int add(int a,int b);

sum=add(n1,n2);

cout<<”sum of two numbers=”<<sum;

getch();

Int add(int a,int b)

{
int c;

c=a+b;

return c;

OUTPUT:
1b)Write a C++ program to find the area of circle such that the class circle must have three member
functions namely :

i)read() to accept the radius from the user

ii)compute() for calculating area of circle

iii)display() to display the result.

Program:

#include<iostream.h>

#include<conio.h>

class circle

float r,a;
public:

void read()

cout<<”Enter the Radius value:\n”;

cin>>r;

void compute()

a=3.14*r*r;

void display()

Cout<<”Area of the circle=”<<a;

};

void main()

clrscr();

circle c;

c.read();

c.compute();

c.display();

getch();

Output:
1c)Write a C++ program to find the area of the Rectangle such that the class Rectangle have three
externally defined member functions such as:

i)read() to accept the radius from user

ii)compute() for calculating area of Rectangle

iii)display() for displaying the result.

Program:

#include<iostream.h>

#include<conio.h>

class Rectangle

float l,b,a;

public:
void read();

void compute();

void display();

};

void Rectangle::read()

cout<<”Enter the length and breadth:\n”;

cin>>l>>b;

void Rectangle::compute()

a=l*b;

void Rectangle::display()

cout<<”Area of Rectangle=”<<a;

void main()

clrscr();

Rectangle r;

r.read();

r.compute();

r.display();

}
Output:

2)Programs based on branching and Looping statements using classes

2a)Write a C++ program to check whether the given number is odd or even using if..else statement in
the class

Program:

#include<iostream.h>

#include<conio.h>

class oddeven
{
int n;

public:
voidcheckoddeven()

cout<<”\nEnter the number:”;

cin>>n;

if (n % 2 == 0)

cout<<”\nGiven number is even”;

else

cout<<”\n Given number is odd”;

};

void main()

clrscr();

oddeven e;

e.checkoddeven();

getch();

2b)Write a c++ program with switch case to add,subtract ,multiply and division by using classes

#include<iostream.h>

#include<conio.h>

classarithoper

intoption,a,b,c;

public:
voidasmd()

cout<<enter two numbers”<<endl;

cin>>a>>b;

cout<< ------Menu-------“<<endl;

cout<<”1.ADD\n”<<”2.Subtract\n”<<”3.Multiply\n”<<”4.Division\n”;

cout<<”\nEnter your choice:”<<endl;

cin>>option;

switch(option)

case 1:

c=a+b;

cout<<”\nAddition of two numbers =”<<c;

break;

case 2:

c=a-b;

cout<<”\nsubtraction of two numbers =”<<c;

break;

case 3:

c=a*b;

cout<<”\nMultiplication of two numbers =”<<c;

break;

case 4:

c=a/b;

cout<<”\nDivision of two numbers =”<<c;


break;

};

void main()

clrscr();

arithoper a;

a.asmd();

getch();

2c)Write a C++ program to calculate the value of the following series using for loop in the class.

S=12+22+32+…..+n2

#include<iostream.h>

#include<conio.h>

class series

intn,I,sum;

public:

void read()

cout<<”Enter the value of n”;

cin>>n;

}
void compute()

sum=0;

fpr(i=1;i<=n;i++)

sum=sum+i*i;

void display()

cout<<”value of the series=”<<sum;

};

void main()

clrscr();

series s;

s.read();

s.compute();

s.display();

getch();

2d) Write a C++ program for printing the following patter by using while loop in the class

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

class pattern

intI,n,in;

voidprintpat()

{
cout<<”Enter a number:”

cin>>n;

cout<<”------Pattern----“;

i=1;

while(i<=n)

cout<<”\n;

in=1;

while(in<=i)

cout<<in;

in=in+1;

I=i+1;

};

void main()

clrscr();
pattern p;

p.printpat();

getch();

3a)Write a c++ program to display the sum and average of numbers by using onedimensional array in
class.

Program:

#include<iostream.h>

#include<conio.h>

classsumavg

IntI,count,sum,arr[50];

floatavg;

public:

void average()

cout<<”\nEnter the number of elements:”;

cin>>count;

cout<<\n”Enter”<<count<<”numbers”;

for(i=0;i<count;i++)

cin>>arr[i];

sum=0;

for(i=0;i<count;i++)

{
sum+=arr[i];

avg=(float)sum/count;

cout<<”\nSum of given numbers=”;<<sum;

cout<<\nAverage of given numbers=”;<<avg;

};

void main()

clrscr();

sumavg a;

a.average();

getch();

Output:
3b)Write a C++ program to add two matrices using multidimensional array with class

Program:

#include<iostream.h>

#include<conio.h>

class Matrix

int x[10][10],y[10][10],sum[10][10],row,col,I,j;

public:

voidgetmatrix()

cout<<”\nEnter number of rows:”<<endl;

cin>>row;

cout<<”\nEnter number of columns:”<<endl;

cin>>col;

for(i=0;i<row;i++)

{
for(j=0;j<col;j++)

cout<<”Enter x[“<<i<<”][“<<j<<”]:”;

cin>>x[i][j];

for(i=0;i<row;i++)

for(j=0;j<col;j++)
{

cout<<”Enter y[“<<i<<”][“<<j<<”]:”;

cin>>y[i][j];

voidputmatrix()

cout<<”\n----------------Matrix x------------------\n”;

for(i=0;i<row;i++)

{
for(j=0;j<col;j++)

cout<<x[i][j]<<” “;

cout<<”\n”;

cout<<”\n----------------Matrix y------------------\n”;

for(i=0;i<row;i++)

{
for(j=0;j<col;j++)

cout<<y[i][j]<<” “;

cout<<”\n”;

}
void add()

for(i=0;i<row;i++)

for(j=0;j<col;j++)

sum[i][j]=x[i][j]+y[i][j];

cout<<endl<<”\nSum of the two matrix is<<endl;

for(i=0;i<row;i++)

for(j=0;j<col;j++)

cout<<sum[i][j]<<” “;

cout<<endl;

};

void main()

Matrix M;

M.getmatrix();

M.putmatrix();
M.add();

getch();

Output:
4)Program to use scope resolution operator. Display the various value of the
same variables declared at different scope levels.

4a)C++ program to show that we can access a global variable using scope
resolution operator :: when there is a local variable with same name

Program:
#include<iostream>
#include<conio.h>
int x; // Global x
int main()
{
Clrscr();
int x = 10; // Local x
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
4)b)C++ program to show that :: can be used to access static members when
there is a local variable with same name

Program:

#include<iostream.h>
#include<conio.h>
class Test
{
static int x;
public:
static int y;
void func(int x)
{
cout << "Value of static x is " << Test::x;

cout << "\nValue of local x is " << x;


}
};

// In C++, static members must be explicitly defined


// like this
int Test::x = 1;
int Test::y = 2;

int main()
{
Test obj;
int x = 3 ;
obj.func(x);

cout << "\nTest::y = " << Test::y;

return 0;
}
4c) Program to access the static member function using the scope resolution (::) operator

Program:
#include <iostream.h>
#include<conio.h>
class ABC
{
public:
// declare static member function
static int fun()
{
int a,b,c;
cout<<”Enter the values of a and b”<<endl;
cin>>a>>b;
c=a+b;
cout << " \n Sum of two numbers= ";
return c;
}
};
void main ()
{
clrscr();
// class_name :: function name
ABC :: fun ();
getch();
}

5)Programs to demonstrate various types of constructors and destructors

5a)Write a C++ program to display the student details using Default constructor

Program:
// defining the constructor within the class

#include <iostream>

class student {

int rno;

char name[10];

double fee;

public:

student()

cout << "Enter the RollNo:";

cin >> rno;

cout << "Enter the Name:";

cin >> name;

cout << "Enter the Fee:";

cin >> fee;

void display()

cout << endl << rno << "\t" << name << "\t" << fee;

};

int main()
{

student s; // constructor gets called automatically


when // we create the object of the class

s.display();

return 0;

5b)Write a C++ program to display the values of two numbers using parameterized constructor

Program:

// CPP program to illustrate parameterized constructors


#include <iostream.h>
#include<conio.h>
class Point
{
private:
int x, y;

public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}

int getX() { return x; }


int getY() { return y; }
};

int main()
{
// Constructor called
Point p1(10, 15);

// Access values assigned by constructor


cout << "p1.x = " << p1.getX()
<< ", p1.y = " << p1.getY();

return 0;
}

5c) write a C++ program to copy the values of constructor by using copy constructor

Program:

#include<iostream.h>

#include<conio.h>

class Demo {

private:

int num1, num2;

public:

Demo(int n1, int n2) {

num1 = n1;

num2 = n2;

Demo(const Demo &n) {

num1 = n.num1;

num2 = n.num2;

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

};

int main() {

Demo obj1(10, 20);

Demo obj2 = obj1;

obj1.display();
obj2.display();

return 0;

5d) Write a C++ program to create the objects using constructors and destroy the objects using
destructors

Program:

#include<iostream.h>
#include<conio.h>
int count=0;
class Test
{
public:
Test()
{
count++;
cout<<"\n No. of Object created:\t"<<count;
}

~Test()
{
cout<<"\n No. of Object destroyed:\t"<<count;
--count;
}
};

main()
{
Test t,t1,t2,t3;
return 0;
}

6) Programs to demonstrate use of public,protected and private scope specifiers

6a) Write a C++ program to calculate the area of a circle using Public scope specifier

Program:

#include<iostream.h>
// class definition
class Circle
{
public:
double radius;

double compute_area()
{
return 3.14*radius*radius;
}

};

// main function
int main()
{
Circle obj;

// accessing public datamember outside class


obj.radius = 5.5;

cout << "Radius is: " << obj.radius << "\n";


cout << "Area is: " << obj.compute_area();
return 0;
}

6b) Write a C++ program to display the age by using private scope specifier

Program:

#include <iostream.h>

#include<conio.h>

// define a class

class Sample {

// private elements

private:

int age;

// public elements

public:

void displayAge(int a) {

age = a;

cout << "Age = " << age << endl;

}
};

int main() {

int ageInput;

// declare an object

Sample obj1;

cout << "Enter your age: ";

cin >> ageInput;

// call function and pass ageInput as argument

obj1.displayAge(ageInput);

return 0;

6c)Write a C++ program to display the value using Protected scope specifier

Program:

#include <iostream.h>

#include<conio.h>

// base class

class Parent

// protected data members

protected:

int id_protected;

};

// sub class or derived class from public base class


class Child : public Parent

public:

void setId(int id)

// Child class is able to access the inherited

// protected data members of base class

id_protected = id;

void displayId()

cout << "id_protected is: " << id_protected << endl;

};

// main function

int main() {

Child obj1;

// member function of the derived class can


// access the protected data members of the base class

obj1.setId(81);

obj1.displayId();

return 0;

7a) Write a C++ program to create simple payroll system by using single inheritance

#include<iostream.h>

#include<conio.h>

classemp {

public:

inteno;

char name[20], des[20];

voidget() {

cout<< "Enter the employee number:";

cin>>eno;

cout<< "Enter the employee name:";

cin>>name;

cout<< "Enter the designation:";

cin>>des;

}
};

class salary : public emp {

floatbp, hra, da, pf, np;

public:

voidget1() {

cout<< "Enter the basic pay:";

cin>>bp;

cout<< "Enter the Humen Resource Allowance:";

cin>>hra;

cout<< "Enter the Dearness Allowance :";

cin>>da;

cout<< "Enter the Profitablity Fund:";

cin>>pf;

voidcalculate() {

np = bp + hra + da - pf;

voiddisplay() {

cout<<eno<< "\t" << name << "\t" << des << "\t" <<bp<< "\t" <<hra<< "\t" << da << "\t" <<pf<< "\t"
<<np<< "\n";

};
voidmain() {

inti, n;

charch;

salary s[10];

clrscr();

cout<< "Enter the number of employee:";

cin>>n;

for (i = 0; i< n; i++) {

s[i].get();

s[i].get1();

s[i].calculate();

cout<< "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";

for (i = 0; i< n; i++) {

s[i].display();

getch();

OUTPUT:
7b)Write a C++ program to calculate area of circle and volume of sphere using multi level inheritance.

Program:

#include<iostream.h>

#include<conio.h>

class Data

protected:

float r;

public:

void read()

{
cout<<”\nEnter the radius of the circle:”;

cin>>r;

};

class Area : public Data

protected:

float area;

public:

void compute()

area=3.14*r*r;

voiddisparea()

cout<<”\nArea of circle is:”<<area;

};

class Volume : public Area

private:

float volume;

public:

void calculate()

{
volume=4*area*r/3;

void display()

cout<<”\nVolume of the Sphere is :”<<volume;

};

void main()

clrscr();

Volume v;

v.read();

v.read();

v.compute();

v.diaparea();

v.calculate();

v.display();

getch();

OUTPUT:
8a)Write a C++ program to calculate area of Rectangle and area of triangle using Multiple inheritance.

Program:

#include<iostream.h>

#include<conio.h>

class Polygon

protected:

int height,width;

public:

void read(int a,int b)


{

height=a;

width=b;

};

class Output

public:

void output(int x)

cout<<”\nArea is”<<x;

};

class Rectangle: public Polygon,public Output

public:

int area()

return(height*width);

};

class Triangle:public Polygon,public Output

public:

int area()
{

return((height*width)/2);

};

void main()

clrscr();

int h,w,choice,a;

cout<<”1.Area of Rectangle\n 2. Area of Triangle\n Enter your choice:”;

cin>>choice;

cout<<”\nEnter Height and Width:”;

cin>>h>>w;

switch(choice)

case 1:

Rectangle r;

r.read(h,w);

a=r.area();

r.output(a);

break;

case 2:

Triangle t;

t.read(h,w);

a=t.area();

t.output(a);
break;

default: cout<<”\nInvalid Choice”;

getch();

8b)Write a C++ program to get square and cube of a number by using Hierarchical Inheritance

Program:

#include<iostream.h>

#include<conio.h>

class Number

private:

int num;

public:

void getNumber(void)

cout<<”Enter an integer number:”;

cin>>num;

int returnNumber(void)

return num;

};
class Square : public Number

public:

int getSquare(void)

int num,sqr;

num=returnNumber();

sqr=num*num;

return sqr;

};

class Cube:public Number

private:

public:

int getCube(void)

int num,cube;

num=returnNumber();

cube=num*num*num;

return cube;

};

void main()

{
Square S;

Cube C;

int sqr,cube;

S.getNumber();

sqr=S.getSquare();

cout<<”\nSquare of “<<S.returnNumber()<<”is:”<<sqr<<endl;

C.getNumber();

cube=C.getCube();

cout<<”\nCube of”<<C.returnNumber()<<”is:”<<cube<<endl;

return 0;

9a)Write a c++ program to add, subtract and multiply two numbers by using constructors in multiple
inheritance
9b) Write a c++ program to display values of variables by using constructor in single inheritance
10a)Write a c++ program to add two complex numbers using a friend function .

Program:
Output:

10b) Write a c++ program to display multiplication and cube of two numbers using inline function
10c)Write a program to demonstrate “this” pointer

Program:
Output:
11)Programs to demonstrate function overloading and

11a) Function overloading with different data types and same number of parameters
11b)Write a C++ program to overload functions with same datatype with different number of arguments
11C)Write a C++ program to demonstrate the concept of function overriding
Practical 12

C++ programs to demonstrate pointers

12a)Write a C++ program to print the address of variable and value of the variable using referencing and
dereferencing operators of pointers

#include<iostream.h>

#include<conio.h>

void main()

{
clrscr();

int a,*p;

a=125;

p=&a;

cout<<a<<endl;

cout<<p<<endl;

cout<<*p<<endl;

getch();

*******************

12b) Write a C++ program to demonstrate increment and decrement operation on pointer variables

Program:

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int a,*a1;

float b,*b1;

a1=&a;

b1=&b;

cout<<”Address of variable a=”<<a1<<endl;

cout<<”Address of variable b=”<<b1<<endl;

a1++ ;

b1--;
cout<<”After increment the address of a=”<<a1<<endl;

cout<<”After decrement the address of b=”<<b1<<endl;

getch();

12)C) Write a C++ program to read and display the values to demonstrate dynamic binding using virtual
functions .

#include<iostream.h>

#include<conio.h>

class Base

protected:

inta,b;

public:

virtual void read()

cout<<”Enter two values:”;

cin>>a>>b;

virtual void display()

cout<<”\nThe values are:”<<a<<”\n”<<b<<”\n”;

};

classSub:public Base

{
protected:

intc,d;

public:

virtual void read()

cout<<”\n Enter four values:”;

cin>>a>>b>>c>>d;

virtual void display()

cout<<”The values are:”<<a<<”\n”<<b<<”\n”<<c<<”\n”<<d;

};

void main()

clrscr();

Base *ptr;

Base b;

Sub s;

ptr=&b;

ptr->read();

ptr->display();

ptr=&s;

ptr->read();

ptr->display();
getch();

12)d) Write a C++ program to swap two numbers using call by reference method using pointers

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

inta,b;

cout<<”Enter two numbers:”

cin>>a>>b;

void swap(int *p1,int *p2);

cout<<”The values of a and b in the main function before calling the swap function are
“<<a<<”and”<<b<<endl;

swap(&a,&b);

cout<<”The values of a and b in main function after calling the swap function are
“<<a<<”and”<<b<<endl;

getch();

void swap(int *p1,int *p2)

int temp;

temp=*p1;

*p1=*p2;

*p2=temp;
cout<<”The values of a and b in the swap function after swapping are “<<*p1<<”and”<<*p2<<endl;

13a) Write a C++ program to copy the contents of the text file into another.

Note:

File-> New -> save as -> in.txt and feed some datas over there and save.

File - > save as -> out.txt which is blank file.

Program:
Output:
Now open the out.txt ;

You might also like