2.docx

  • Uploaded by: Hasnain Ali
  • 0
  • 0
  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 2.docx as PDF for free.

More details

  • Words: 1,860
  • Pages: 51
UOG Assignment No: 2 Object Oriented Programming

Submitted By: Name: Hasnain Ali Roll No: 18134156-022

Section: IT-18-A

Submitted To: Sir. Junaid Muzaffar

1

Q1. #include #include #include<math.h> using namespace std; struct complex { float rel; float img; }s1,s2; int main() 2

{ cout<<"\n\n\t\t\tWelcome to the imaginary system"; float a,b; cout<<"\n\n\t\tEnter real and imaginary part of 1st complex number:"; cin>>s1.rel>>s1.img; cout<<"\n\n\t\tEnter real and imaginary part of 2nd complex number:"; cin>>s2.rel>>s2.img; //Addition a=(s1.rel)+(s2.rel); b=(s1.img)+(s2.img); cout<<"\n\n\t\t Addition: "<<"("<
3

//Subtraction a=(s1.rel)-(s2.rel); b=(s1.img)-(s2.img); cout<<"\n\n\t\tSubtraction: "<<"("<
4

cout<<"\n\n\t\tDivision: "<<"("< #include using namespace std; struct stud { int roll; string nm; float m1, m2, m3; }; typedef stud S; 5

int main() { S student[25]; for(int i =0 ; i < 25 ; i++) { cout << "\n Enter Roll no : "; cin >> student[i].roll; cout << "\n Enter Name : "; cin>> student[i].nm; cout << "\n Enter marks of three subjects :"; cin >> student[i].m1 >> student[i].m2 >> student[i].m3 ; 6

} cout<< "\n STUDENTS FAILED IN MORE THAN 1 SUBJECT \n "; for(int i =0 ; i < 25 ; i++) { if(( student[i].m1< 40 && student[i].m2 < 40) || (student[i].m2 < 40 && student[i].m3 < 40) || ( student[i].m1 < 40 && student[i].m3 < 40)) cout << student[i].roll << "\t" << student[i].nm << "\n"; } getch(); }

7

Q3. Private members : adno 4 digit admission number name 20 characters marks an array of 5 floating point values average average marks obtained GETAVG() a function to compute the average obtained in five subject Public members: READINFO() function to accept values for adno, name, marks. Invoke the function GETAVG() DISPLAYINFO() function to display all data members of report on the screen.

8

#include #include #include<stdio.h> using namespace std; class student { private: int admno; char sname[20]; float eng,math,science; float total; float ctotal() { return eng+math+science; } 9

public: void Takedata() { cout<<"Enter admission number "; cin>> admno; cout<<"Enter student name " ; gets(sname); cout<< "Enter marks in english, math, science "; cin>>eng>>math>>science; total=ctotal(); } void Showdata() {

10

cout<<"Admission number "<
} Q4. #include #include #include<stdio.h> using namespace std; class TEST { private: int TestCode; char Description[30]; int NoCandidate; int CenterReqd; int CALCNTR() { return NoCandidate/100+1; 12

} public: void SCHDULE(); void DISPTEST(); }; void TEST::SCHDULE() { cout<<"Enter Test code "; cin>> TestCode; cout<<"Enter description "; gets(Description); cout<< "Enter no of candidates "; cin>>NoCandidate; CenterReqd=CALCNTR(); } 13

void TEST :: DISPTEST() { cout<<"Test code "<
} Q5. 1. Write the definitions for each of the above member functions. 2. Write main function to create two rectangle objects. Set the length and width of the first rectangle to 5 and 2.5. Set the length and width of the second rectangle to 5 and 18.9. Display each rectangle and its area and perimeter. 3. Check whether the two Rectangles have the same area and print a message indicating the result. Set the length and width of the first rectangle to 15 and 6.3. Display each Rectangle and its area and perimeter again. Again, check whether the two Rectangles have the same area and print a message indicating the result #include 15

#include using namespace std; class Rectangle { private: float length; float width; public: void setlength(float); void setwidth(float); float perimeter(); float area(); void show(); int sameArea(Rectangle); }; 16

void Rectangle::setlength(float len) { length = len; } void Rectangle::setwidth(float wid) { width = wid; } float Rectangle::perimeter() { return (2 * length + 2 * width); } 17

float Rectangle::area() { return length * width; } void Rectangle::show() { cout << "Length: " << length << " Width: " << width; } int Rectangle::sameArea(Rectangle other) { float areaf = length * width; float areas = other.length * other.width; 18

if (areaf == areas) return 1; return 0; } int main() { Rectangle first; Rectangle second; first.setlength(5); first.setwidth(2.5); second.setlength(5); second.setwidth(18.9); cout << "First rectangle: "; first.show();

19

cout << endl << "Area: " << first.area() << "Perimeter: " << first.perimeter() << endl << endl; cout << "Second rectangle: "; second.show(); cout << endl << "Area: " << second.area() << "Perimeter: " << second.perimeter() << endl << endl; if (first.sameArea(second)) cout << "Rectangles have the same area\n"; else cout << "Rectangles do not have the same area\n"; first.setlength(15); first.setwidth(6.3); cout << "First rectangle: "; 20

first.show(); cout << endl << "Area: " << first.area() << "Perimeter: "<< first.perimeter() << endl << endl; cout << "Second rectangle: "; second.show(); cout << endl << "Area: " << second.area() << "Perimeter: "<< second.perimeter() << endl << endl; if (first.sameArea(second)) cout << "Rectangles have the same area\n"; else cout << "Rectangles do not have the same area\n"; getch(); return 0; 21

} Q6. #include #include using namespace std; class Distance { private: int feet; float inches; public: void setdist(int ft, float in) { feet=ft; inches=in; } 22

Distance add(Distance); void disp(); }; Distance Distance::add(Distance D) { Distance t; t.inches=inches + D.inches; t.feet =0; if(t.inches>=12.0) { t.inches-=12.0; t.feet++; } t.feet +=feet + D.feet; return t; 23

} void Distance::disp() { cout<
int main() { Distance d1,d2,d3; d1.setdist(10,7.1); d2.setdist(23,5.5); d3=d1.add(d2); cout<<"\n distance 1 = ";d1.disp(); 24

cout<<"\n distance 2 = ";d2.disp(); cout<<"\n distance 3 = ";d3.disp(); getch(); return 0; } Q7. Define a class to represent a bank account. It contains Data Members: Name of the depositor Account Number Type of account Balance Member Functions: To assign initial values To deposit an amount To withdraw an amount < amount available Display the name and balance. #include 25

#include #include<string.h> using namespace std; class bank { char name[20]; int ano; char atype[20]; float bal; public: void get(int no,char *n,char *t,float b) { strcpy(name,n); ano=no; strcpy(atype,t); bal=b; 26

} float deposit() { float amt; cout<<"\nEnter amount: "; cin>>amt; bal=bal+amt; return bal; } float withdrw() { float amt; cout<<"\nHow many Rupees withdraw: "; cin>>amt; bal=bal-amt; return bal; 27

} void disp() { cout<<"\n\nAccount number: "<
bank bk; cout<<"\nEnter Account no.: "; cin rel="nofollow">>n; cout<<"\nEnter Name: "; cin>>nm; cout<<"\nEnter account type: "; cin>>t; cout<<"\nEnter balance amount: ";cin>>a; bk.get(n,nm,t,a); bk.disp(); getch(); } Q8. To write a C++ program to display the student details using class and array of object. Algorithm: class as student. data members rollno, name, mark1, mark2, mark3, total and average. 29

member functions as getdata() and displaydata(). getdata() method used to get the student details. displaydata() method used to display the student details. create an object array for the student class using the following syntax: Get the number of students. Enter student details display the student details #include #include using namespace std; class Result{ int rollNo,marks[3]; char name[50]; public: void input(); 30

void show(); void total(); float avg(); }; void Result::input(){ cout<<"Enter Roll No"<<endl; cin>>rollNo; cout<<"Enter Name"<<endl; cin>>name; for(int i=0; i<4;i++){ cout<<"Enter Marks"<<<"Book"<<endl; cin>>marks[i]; } } void Result::show(){ 31

cout<<"Roll No "<
summ=summ+marks[i]; } cout<<"Average"<<summ/4.0<<endl; //float avgr; //for(int i=1;i<4;i++){ //sum=sum+marks[i]; //} //avgr=sum/3; //cout<<"Avrage "<
getch(); } Q10. Write a C++ program to add two complex numbers. i) The class Complex contains three constructors. a) One with no parameter. (Used for the object for storing result.) b) With one parameter(Same value for real and imaginary part) c) With two parameters. and ii) Two friend functions a) One to add two complex number by taking two reference variables of class complex and returning another reference. b) To display the result. #include using namespace std; 34

class complex { float x; float y; public : complex() //no argument constructor { /* cout<<"Enter real = "; cin>>x; cout<<"Enter imaginary = "; cin>>y;*/ } complex(float real, float imag) {

35

cout<<"Enter real = "; cin>>x; cout<<"Enter imaginary = "; cin>>y; x = real; y = imag; } complex operator+(complex); void display(void); }; complex complex :: operator+(complex c) { complex temp; temp.x = x + c.x; temp.y = y+c.y; 36

return(temp); } void complex :: display(void) { cout<<x<<" +i"<
Q12. Check whether a number is even or odd by overloading ! operator. #include using namespace std; int main() { int n; cout << "Enter an integer: "; cin >> n; if ( n % 2 == 0) cout << n << " is even."; else cout << n << " is odd."; 38

return 0; } Q13. Check whether a number is prime or not by overloading – operator #include using namespace std; int checkPrimeNumber(int); int main() { int n; cout << "Enter a positive integer: "; cin >> n; 39

if(checkPrimeNumber(n) == 0) cout << n << " is a prime number."; else cout << n << " is not a prime number."; return 0; } int checkPrimeNumber(int n) { bool flag = false; for(int i = 2; i <= n/2; ++i) { if(n%i == 0) { flag = true; 40

break; } } return flag; } Q14. Add two complex number by overloading + operator a) Using Member function. b) Using Friend Function. #include using namespace std; class complex {

41

int real,imag; public: void set() { cout<<"enter real and imag part"; cin>>real>>imag; } friend complex sum(complex,complex);

42

void display(); }; void complex::display() { cout<<"the sum of complex num is"<
complex t; t.real=a.real+b.real; t.imag=a.imag+b.imag; return t; } int main() { complex a,b,c; 44

a.set(); b.set(); c=sum(a,b); c.display(); return(0); } Q15. Concatenate two strings by overloading + operator. a) Overload ++ as prefix (++c1) and postfix (c1++) in some class. b) Overload == operator to compare two strings. 45

#include using namespace std; class Complex { private: int real; int imag; public: Complex(int r, int i) { real = r; imag = i; } Complex operator ++(int); Complex & operator ++(); }; Complex &Complex::operator ++() { real++; imag++; 46

return *this; } Complex Complex::operator ++(int i) { Complex c1(real, imag); real++; imag++; return c1; } int main() { Complex c1(10, 15); c1++; ++c1; return 0; 47

} Q16. Write a program to convert a distance entered in Feet and Inches to Meter using class to basic data type conversion. #include using namespace std; class Distance { private: int feet; float inch; public: Distance(); Distance(int a,float b); void setDistance(); 48

int getFeet(); float getInch(); void distanceSum(Distance d); }; int main() { Distance D1,D2; D1.setDistance(); D2.setDistance(); D1.distanceSum(D2); return 0; } /*Function Definitions*/ Distance::Distance() { inch=feet=0; 49

} Distance::Distance(int a,float b) { feet=a; inch=b; } void Distance::setDistance() { cout<<"Enter distance in feet"; cin>>feet; cout<<endl<<"Enter inches:"; cin>>inch; } int Distance::getFeet() { return feet; 50

} float Distance::getInch() { return inch; } void Distance::distanceSum(Distance d) { cout<<"feet="<
Accounting .pdf
April 2020 4
2.docx
April 2020 9
Accounting1 .pdf
April 2020 5
Abbas And Taj Mahal
October 2019 25