Nafees 1
Nafees 1
Nafees 1
#include <iostream>
using namespace std;
struct phone
{
int area; //area code (3 digits)
int exchange; //exchange (3 digits)
int number; //number (4 digits)
};
int main()
{
phone ph1 = { 212, 767, 8900 }; //initialize phone number
phone ph2; //define phone number
cout << "\nMy number is " << '(' << ph1.area << ") " <<
ph1.exchange << '-' << ph1.number;
cout << "\nYour number is " << '(' << ph2.area << ") "<<
ph2.exchange << '-' << ph2.number << endl;
return 0;
Solution 2:
#include <iostream>
using namespace std;
struct point
{
int xCo; //X coordinate
int yCo; //Y coordinate
};
int main()
{
point p1, p2, p3; //define 3 points
cout << “\nEnter coordinates for p1: “; //get 2 points
cin >> p1.xCo >> p1.yCo; //from user
cout << “Enter coordinates for p2: “;
cin >> p2.xCo >> p2.yCo;
p3.xCo = p1.xCo + p2.xCo; //find sum of
p3.yCo = p1.yCo + p2.yCo; //p1 and p2
cout << “Coordinates of p1+p2 are: “ //display the sum
<< p3.xCo << “, “ << p3.yCo << endl;
return 0;
}
Solution 3:
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle( double = 1.0, double = 1.0 );
double perimeter( void );
double area( void );
void setWidth( double w );
void setLength( double l );
double getWidth( void );
double getLength( void );
private:
double length;
double width;
};
int main()
{
Rectangle a, b( 4.0, 5.0 ), c( 67.0, 888.0 );
// output Rectangle a
cout << "a: length = " << a.getLength()<< "; width = " <<
a.getWidth()<< "; perimeter = " << a.perimeter() << "; area = " <<
a.area() << '\n';
// output Rectangle b
cout << "b: length = " << b.getLength() << "; width = " <<
b.getWidth() << "; perimeter = " << b.perimeter() << "; area = " <<
b.area() << '\n';