0% found this document useful (0 votes)
189 views

Oop Practical

Here is a C++ program to define a Distance class with a data member meters and display the addition of two Distance objects using a friend function: #include <iostream> using namespace std; class Distance { int meters; public: Distance(int m) { meters = m; } friend Distance addDistance(Distance, Distance); }; Distance addDistance(Distance d1, Distance d2) { Distance result; result.meters = d1.meters + d2.meters; return result; } int main() { Distance d1(10), d2(5), sum; sum = addDistance(d
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)
189 views

Oop Practical

Here is a C++ program to define a Distance class with a data member meters and display the addition of two Distance objects using a friend function: #include <iostream> using namespace std; class Distance { int meters; public: Distance(int m) { meters = m; } friend Distance addDistance(Distance, Distance); }; Distance addDistance(Distance d1, Distance d2) { Distance result; result.meters = d1.meters + d2.meters; return result; } int main() { Distance d1(10), d2(5), sum; sum = addDistance(d
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/ 56

¢0MrUTER GR0Ur l SEMESTER - III ț OIrLOMx IN Ea¢laEERlNG AND TECHxOLOGY

PR ’++
’æ *’
’**W• #’
_ .'
',\/ \/- ’’ -
- ’- ' . '
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
Certificate
. ... . .... .................................
ROI1N O.. of ThirdSemesterofDiplomain

(Code: .... .... . ...) has completed ihc term work satisfactorily in course
Ob)ert Orlenied Programming Using C + (22316} tor the academic year
20........ to 20.as prescribed in ihe curriculum.

Place: . . . . . . . . . . . . . . . EOFOllMORt TO :.. . . . . . . . . . . . . . .


Date: ........ . .. .. . .
9

9 -.
9
C
//Exercise:

//1] Write a C++ Program to check leap year

#include <iostream.h>

#include <conio.h>

Void main()

Int year;

Clrscr();

Cout<<” Output prepared by Khan Abdullah Riyaz\n”;

Cout<<”Enter the year: “;

Cin>> year;

If (year % 4 == 0)

Cout<< year <<” is a leap year”;

Else

Cout<<year<<” is not a leap year”;

Getch();

}
//2) Write a C++ Program armstrong between two numbers

#include <iostream.h>

#include <conio.h>

#include <math.h>

Int main()

//Decalring the variables for the

program Int num1, num2, I, num, digit,

sum;

//Taking input number first for the starting range

Cout<<endl<<”Output prepared by Khan Abdullah Riyaz”<<endl;

Cout<<”Enter first number: “;

Cin>>num1;

//Taking input number second for the ending range

Cout<<”Enter second nunber: “;

Cin>>num2;

//Finding the Armstrong numbers in the input range

Cout<< “ Armstrong numbers between “<<num1<< “ and “<<num2<<”are: “<<endl;

For (I = num1; i<= num2; i++)

Sum =0;

Num =I;

For (; num > 0; num /= 10)

Digit = num % 10;

Sum = sum + digit * digit * digit;


}

If (sum == i)

//preparing the armstrong numbers

Cout<<i<<endl;

Return 0;

//3)

#include <iostream.h>

#include <conio.h>

Int main()

Int num, rev = 0, rem, temp;


Cout<<endl<<”Output Prepared by Khan Abdullah Royaz”<<endl;

Cout<<”Enter the number\n: “;

Cin>>num;

Temp = num;

While (temp>0)

Rem = temp%10;

Rev = (rev*10)+rem;

Temp = temp/10;

If (rev == num)

Cout<<”\nit is a palindrome number”;

Else

Cout<<”\nit iz not a palindrome number”;

Cout<<endl;

Return 0;

}
,y

L
//EXercise

//1) Write A C++ Program to print a multiplication table of 7

#include<iostream.h>

int main()

int num, i, tab;

num=7;

cout<<"Table of "<<num<<" is \n\n";

for(i=1; i<=10; i++)

tab=num*i;

cout<<num<<" * "<<i<<" = "<<tab<<"\n";

}
//2) Write A C++ Program to print 100 to 1

#include <iostream.h>

#include <conio.h>

Void main()

Clrscr();

Int I;

Cout<<”100 to 1 numbers : “<<endl;

For (i=100; i>=1; i--)

Cout<<i<<endl;

Getch();

}
//3] Wrote a C++ Program which will print (half pyramid) pttern of the natural numbers

#include <iostream.h>

#include <conio.h>

Void main()

Clrscr();

Int rows =

5; Int I;

Int j;

Cout<<”half pyramid of natural numbers “<<endl;

For (int I = 1; j<= rows; i++)

For (j = 1; j <= I; j++)

Cout<< j<< “ “;

Cout<< “\n”;

Getch();

}
//4] Write A C++ program ask the user to enter the number of rows to print the pyramid of stars(*)

#include <iostream.h>

#include <conio.h>

Int main()

Int rows;

Cout << “Enter number of rows: “;

Cin >> rows;

For(int I = 1; I <= rows; ++i)

For(int j = 1; j <= I; ++j)

Cout << “* “;

Cout << “\n”;


}

Getch();

}
0

° **1 ”"

S/z- r*° “ È" *’“° ”*

zoo y•• ‘l ’“ L
//Exercise

//1) Write a C++ program to declare a class “Book” having data members book_name, author and
price. Accept and display data for book having maximum price.

#include<iostream.h>

#include<conio.h>

#include<string.h>

Class Book

Int no_of_pages;

Char book_name[50];

Public:

Float price;

Void getdata()

Cout<<”Enter Book name:-> “;

Cin>>book_name;

Cout<<”Enter Book Price:->”;

Cin>>price;

Cout<<”Enter No of pages:-> “;

Cin>>no_of_pages;

}
Void display()

Cout<<”\nBook name:-> “<<book_name;

Cout<<”\nBook Price:->”<<price;

Cout<<”\nNo of pages:-> “<<no_of_pages;

};

Void main()

Book b1,b2;

Clrscr();

B1.getdata();

B2.getdata();

If(b1.price>b2.price)

B1.display();

Else

B2.display();

Getch();

}
//2) Write a C++ program to declare a class “staff having data members name. basic salary, DA, HRA
and calculate gross salary. Accept and display data for one staff.

// a. Where DA-74.5% of basic

// i.HRA 30% of basic.

// ii. Gross_salary-basic+HRA+DA

#include<iostream.h>

#include<conio.h>

Class staff

{
Float
basic,gross_sal;

Public:

Void accept()

Cout<<endl<<”Output Prepared By Khan Abdullah Riyaz”<<endl;

Cout<<”Enter Basic Salary of Staff: “;

Cin>>basic;

Float display ();

};

Float staff :: display()

Float DA=74.5, HRA=30;

Gross_sal=(basic*DA/100)+(basic*HRA/100)+basic;

Return(gross_sal);

Void main()

Clrscr();

Staff s1;

S1.accept();

Cout<<”Gross Salary of Employee “<<s1.display();

Getch();

}
Output Prepared By Khan Abdullah Rżyaz
Enter Basic Salary of Staff: 30000
Gross Salary of Employee 61350
[Program finished]
› ‹ . . -”i- » ”<
0

re.• cc • r-* ° °

_‹.. a.-=›. •î
*“ "’*
//Exercise

//1) Write a C++ program to define a class “Employee” having data members emp_id, emp_name and
emp_salary. Accept and display data for employees having salary greater than 25,000/-.

#include<iostream.h>

#include<conio.h>

Class Employee

Int emp_id;

Char emp_name[20];

Public:

Float emp_salary;

Void accept()

Cout<<”\nEnter Employee id=> “;

Cin>>emp_id;

Cout<<”Enter employee Name=> “;


Cin>>emp_name;

Cout<<”Enter employee salary=> “;

Cin>>emp_salary;

Void display()

Cout<<endl<<”Employee id=> “<<emp_id<<endl;

Cout<<”Employee Name=> “<<emp_name<<endl;

Cout<<”Employee Salary=> “<<emp_salary;

};

Void main()

Int I;

Clrscr();

Employee e[2];

For(i=0;i<2;i++)
{

E[i].accept();

Cout<<”\nEmployees having Salary greater than 25000 are:\n”;

For(i=0;i<3;i++)

If(e[i].emp_salary>25000)

E[i].display();

Getch();

}
//2)Write a C++ program to define a class “City” having data members name, population.Accept and
display data for 10 cities.

#include<iostream.h>

#include<conio.h>

Class City

Long int population;

Char city_name[20];

Public:

Void accept()

Cout<<endl<<”Output orepared by Khan Abdullah Riyaz”<<endl;

Cout<<”\nEnter City Name=> “;

Cin>>city_name;

Cout<<”Enter Population of the City=> “;

Cin>>population;

Void display()

{
Cout<<endl<<”City Name=> “<<city_name<<endl;

Cout<<”Population=> “<<population<<endl;

};

Void main()

Int I;

Clrscr();

City c[2];

For(i=0;i<2;i++)

C[i].accept();

Cout<<”\nCity details are:\n”;

For(i=0;i<2;i++)

{
C[i].display();

Getch();

}
0
0

fZ•‹ • ‹ ,_l tl \* I *‘ '*


9

c>• 1-
// 1) C++ Program to swap two numbers using friend function

#include <iostream.h>

#include <conio.h>

Class Swap {

// Declare the variables of Swap Class

Int temp, a, b;

Public:

// Define the parameterized constructor, for inputs

Swap(int a, int b)

This->a = a;

This->b = b;

}
// Declare the friend function to swap, take arguments

// as call by reference

Friend void swap(Swap&);

};

// Define the swap function outside class scope

Void swap(Swap& s1)

// Call by reference is used to passed object copy to

// the function

Cout<<endl<<”output prepared by khan abdullah riyaz”<<endl;

Cout << “\nBefore Swapping: “ << s1.a << “ “ << s1.b;

// Swap operations with Swap Class variables

S1.temp = s1.a;

S1.a = s1.b;

S1.b = s1.temp;
Cout << “\nAfter Swapping: “ << s1.a << “ “ << s1.b;

// Driver Code

Int main()

// Declare and Initialize the Swap object

Swap s(4, 6);

Swap(s);

Return 0;

}
//2) Write a C++ program to define a class “Distance” having data member as meters.Display the
addition of two Distance objects using friend function.

#include <iostream.h>

#include <conio.h>

Class DM

Float distm; //distance in meters

Public:

DM()

Cout<<”\n\tEnter ditance in meters:->”;

Cin>>distm;

Friend void totaldist(DM d1, DM d2);

};

Void totaldist(DM d1, DM d2)

Float dist =d1.distm + d2.distm;

Cout<<”\nTotal Distance in Meters => “<<dist;

Cout<<endl<<”output prepared by abdullah riyaz”<<endl;

Void main()

{
Clrscr();

DM d1;

DM d2;

Totaldist(d1,d2);

Getch();

}
!1


9
9

6
L*
9

a
0
.
Duted signature
Marks Obtained
of Teacher
Process Product
Total(50)
Related(35) Related(IS)

You might also like