0% found this document useful (0 votes)
14 views17 pages

Assignment M.shehzad.....

Uploaded by

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

Assignment M.shehzad.....

Uploaded by

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

(OOP)

(Object Oriented Programming)

Name M.SHEHZAD

class Bs(it)

Assignment Object Oriented


Programming(OOP)
Submitted To Mam Pimra

25-05-2023 Page 1
(OOP)

 Program No 1:
Q1: Write a C++ program that take input from user in mile
and convert it into kilometers by using a class named
conversion? (Remember: 1 mile = 1.6 kilometers ) .

#include <iostream>

using namespace std;

class Conversion

public:

float milesToKilometers(float miles)

return miles * 1.6;

};

int main()

float miles;

cout << "Enter the distance in miles: ";

cin >> miles;

Conversion converter;

25-05-2023 Page 2
(OOP)

float kilometers = converter.milesToKilometers(miles);

cout << miles << " miles is equal to " << kilometers << " kilometers." << endl;

return 0;

 Output :

Enter the distance in miles: 200

200 miles is equal to 320 kilometers.

--------------------------------

Process exited after 22.1 seconds with return value 0

Press any key to continue . . .

25-05-2023 Page 3
(OOP)

 Program No 2 :

Q2: Write a C++ program to compare two strings using


overloading.

#include<iostream>

using namespace std;

#include<string>

class stringcomparator

public:

bool operator ()(const string& str1,const string& str2 )

return str1== str2;

};

int main ()

string str1,str2;

cout<<"Enter frist string";

cin >> str1;

cout<<"Enter second string";

25-05-2023 Page 4
(OOP)

cin>> str2;

stringcomparator comparator;

bool areEqual = comparator(str1, str2);

if (areEqual)

cout<<"string are equal";

else

cout<<"string are not equal";

return 0;

 Output :

Enter frist string shehzad

Enter second string shehzad

string are equal

--------------------------------

Process exited after 40.68 seconds with return value 0

Press any key to continue . .

25-05-2023 Page 5
(OOP)

 Program No 3 :

Q3 : Write a C++ program to display date using


constructors.

#include<iostream>

using namespace std;

class date

int day;

25-05-2023 Page 6
(OOP)

int month;

int year;

public:

date(int d,int m,int y)

day = d;

month = m;

year = y;

void displaydate()

cout<<"Date"<<day<<"/"<<month<<"/"<<year<<"/"<<endl;

};

int main()

int day, month, year;

cout<<"Enter day";

cin>> day;

cout<<"Enter month";

cin>> month;

cout<<"Enter years";

cin>> year;

25-05-2023 Page 7
(OOP)

date date(day,month,year);

date.displaydate();

return 0;

 Output :

Enter day 19

Enter month 06

Enter years 2004

Date19/6/2004/

--------------------------------

Process exited after 64.91 seconds with return value 0

Press any key to continue . . .

25-05-2023 Page 8
(OOP)

 Program no 4 :

Q4: Write a C++ program to find largest of three numbers


using class.

#include<iostream>

using namespace std;

class NumberComparator

public:

int largest(int num1,int num2,int num3)

int largest = num1;

if (num2>largest)

largest = num2;

if (num3>largest)

largest = num3;

return largest;

25-05-2023 Page 9
(OOP)

};

int main()

int num1;

int num2;

int num3;

cout<<"Enter first number";

cin>>num1;

cout<<"Enter second number";

cin>>num2;

cout<<"Enter third nmber";

cin>>num3;

NumberComparator comparator;

int largestNumber = comparator.largest(num1, num2, num3);

cout << "The largest number is: " << largestNumber << endl;

return 0;

25-05-2023 Page 10
(OOP)

 Output:

Enter first number41

Enter second number58

Enter third nmber66

The largest number is: 66

--------------------------------

Process exited after 24.05 seconds with return value 0

Press any key to continue . . .

25-05-2023 Page 11
(OOP)

 Program NO 5 :

Q5: Write the member function to overload the == operator.


Use the standard == operator to compare the weightDate
data member of the ZooAnimal object to the integer
parameter.

#include<iostream>

using namespace std;

class ZooAnimal

int weightDate;

public:

ZooAnimal(int weight)

weightDate = weight;

25-05-2023 Page 12
(OOP)

bool operator==(int weight)

return weightDate == weight;

};

int main()

int weight;

cout<<"Enter weight";

cin>>weight;

ZooAnimal animal(400);

if (animal == weight)

cout<<"The weight matchs"<<endl;

else

cout<<"The weight is does not match"<<endl;

return 0;

25-05-2023 Page 13
(OOP)

 Output :

Enter weight 401

The weight is does not match

--------------------------------

Process exited after 37.35 seconds with return value 0

Press any key to continue . . .

 Program no 6 :
25-05-2023 Page 14
(OOP)

Q6 : Write a function called swap() that interchanges two


int values passed to it by the calling program. (Note that this
function swaps the values of the variables in the calling
program, not those in the function.) You’ll need to decide
how to pass the arguments. Create a main() program to
exercise the function.

#include<iostream>

using namespace std;

void swap(int& num1, int& num2)

int temp = num1;

num1 = num2;

num2 = temp;

int main()

int a;

int b;

cout<<"Enter fitst number";

cin>>a;

25-05-2023 Page 15
(OOP)

cout<<"Enter second number";

cin>>b;

cout<<"Before swaping:a ="<<a<<",b = " <<b<<endl;

swap(a,b);

cout<<"After swaping: a = " <<a<< ",b = "<<b<<endl;

return 0;

 Output :

Enter fitst number 47

Enter second number55

Before swaping:a =47,b = 55

After swaping: a = 55,b = 47

--------------------------------

Process exited after 25.45 seconds with return value 0

Press any key to continue . . .

25-05-2023 Page 16
(OOP)

25-05-2023 Page 17

You might also like