Sei sulla pagina 1di 8

 

Object Oriented programming(SCT155)


                                                                Assignment-1
 
 
 
 
 
 

 
 
 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Submitted by:                                                                         Submitted to:
 name: Shubham kumar ranaut                                          MISS. SANDEEP KAUR BAINS
 UID: 18bsc1010
 
 
Q1. Write a program that generates the following output:
10
20
19
      Use an integer constant for the 10, an arithmetic assignment operator to generate 
the 20, and a decrement operator to generate the 19.
 
ANS:
#include <iostream>
using namespace std;
int main()
{
       int var = 10;
       cout << var << endl;      // var is 10
       var *= 2;                 // var becomes 20
       cout << var-- << endl;    // displays var, then decrements it
       cout << var << endl;      // var is 19
       return 0;
 
}
OUTPUT SCREEN:
 
10                                                                                                                               
20                                                                                                                               
19                                                                                                                               
                                                                                                                                 
                                                                                                                                 
...Program finished with exit code 0                                                                                             
Press ENTER to exit console.  
 
 
Q2. In the heyday of the British empire, Great Britain used a monetary system based on pounds, shillings, and pence. 
There were 20 shillings to a pound, and 12 pence to a shilling. The notation for this old system used the pound sign, £, 
and two decimal points, so that, for example, £5.2.8 meant 5 pounds, 2 shillings, and 8 pence. (Pence is the plural of 
penny.) The new monetary system, introduced in the 1950s, consists of only pounds and pence, with 100 pence to a 
pound (like U.S. dollars and cents). We’ll call this new system decimal pounds. Thus £5.2.8 in the old notation is 
£5.13 in decimal pounds (actually £5.1333333). Write a program to convert the old pounds-shillings-pence format to 
decimal pounds. An example of the user’s interaction with the program would be
Enter pounds: 7
Enter shillings: 17
Enter pence: 9
Decimal pounds = £7.89
      In most compilers you can use the decimal number 156 (hex character constant ‘\x9c’) to represent the pound 
sign (£). In some compilers, you can put the pound sign into your program directly by pasting it from the Windows 
Character Map accessory.
 
ANS:                                                                                          
 
#include <iostream>                                                                                                                                                          
 
#include <stdlib.h>
 
using namespace std;
 
int main(){
 
       double Pounds, Shillings, Pence;
 
       cout << "Enter Pounds: ";         cin  >> Pounds;
 
       cout << "Enter Shillings: ";      cin  >> Shillings;
 
       cout << "Enter Pence: ";          cin  >> Pence;
 
       Pounds = Pounds + (Shillings/20) + (Pence/20/12);
 
       cout << "Decimal Pounds = "<<'\x9c'<<Pounds << endl;
 
 
       return 0;
 
}
OUTPUTSCREEN:
Enter Pounds: 7                                                                                                                
Enter Shillings: 17                                                                                                            
Enter Pence: 9                                                                                                                 
Decimal Pounds = �7.8875                                                                                                       
                           
 
Q3. Write a program that uses a structure called point to model a point. Define three 
points, and have the user input values to two of them. Then set the third point equal to 
the sum of the other two, and display the value of the new point. Interaction with the 
program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+p2 are: 8, 11
ANS:
  #include<iostream>
using namespace std;
class point

    public:
    int x,y;
};
int main()
{
point p1,p2,p3;
cout<<"Enter coordinates for P1: ";
cin>>p1.x>>p1.y;
cout<<"Enter coordinates for P2: ";
cin>>p2.x>>p2.y;
p3.x=p1.x+p2.x;
p3.y=p1.y+p2.y;
cout<<"Coordinates of P1 + P2 are: "<<p3.x<<" "<<p3.y;
return 0;
}
output screen:
Enter coordinates for P1: 3 4                                                                                                  
Enter coordinates for P2: 5 7                                                                                                  
Coordinates of P1 + P2 are: 8 11                                                                                               
                                                                                                                               
...Program finished with exit code 0                                                                                           
Press ENTER to exit console.  
Q4. Write a function that, when you call it, displays a message telling how many times it 
has been called: “I have been called 3 times”, for instance. Write a main() program that 
calls this function at least 10 times. Try implementing this function in two different 
ways. First, use a global variable to store the count. Second, use a local static variable. 
Which is more appropriate? Why can’t you use a local variable?
ANS:
#include<iostream>
#include<conio.h>
using namespace std;

void caller_counter(void);

int main()
{

int outer_counter=0;

do{
outer_counter++; caller_counter();
cout<<"\nThe main programme counter value is: "<<outer_counter;
cout<<"\n\n !Press c to continue or any key to exit."<<endl<<endl;
}while(getch()=='c');
}

void caller_counter(void){
//I can't use an automatic variable coz it's created once I call the function only.
static int inner_counter=0; inner_counter++;
cout<<"I have been called "<<inner_counter<<" times";}
OUTPUTSCREEN:
I have been called 1 times                                                                                                       
The main programme counter value is: 1                                                                                           
                                                                                                                                 
 !Press c to continue or any key to exit.                                                                                        
                                                                                                                                 
I have been called 2 times                                                                                                       
The main programme counter value is: 2                                                                                           
                                                                                                                                 
 !Press c to continue or any key to exit.
Q5. Create a class phone having data members: 1. The STD code 2. The Exchange code 
3.Phone Number Ex :- 212-766-8901 Write a C++ program to accept details from user 
(max 10) and change input phone number to new phone number using following criteria:
 a) Add 1 to 1st digit of STD code. (If digit is 9 it becomes 10)
 b) The exchange code is retained as it is. 
 c) In 3rd part of structure, 1st two digits should be reversed. Ex: I/P : 212-766-890 => 
O/P : 312-766-980 Display all changed phone numbers.
ANS:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
using namespace std;

class phone
{
        long int std, ex, phno;
    public:
        void accept();
        void change();
        void display();
};
void phone::accept()
{
        cout<<"\n Enter STD Code        : ";
        cin>>std;
        cout<<"\n Enter Exchange Code   : ";
        cin>>ex;
        cout<<"\n Enter Phone Number    : ";
        cin>>phno;
}
void phone::display()
{
        cout<<" ";
        cout<<std<<"-"<<ex<<"-"<<phno;
}
void phone::change()
{
        long int s, ph, rev, fact;
        int cnt;
        s = std;
        ph = phno;
        while(s!=0)
        {
                s=s/10;
                cnt++;
        }
        fact=pow(10,(cnt-1));
        std=std+fact;
        while(ph!=0)
        {
                ph=ph/10;
                cnt++;
        }
        fact=pow(10,(cnt-2));
        rev=phno/fact;
        rev=(rev%10)*10+rev/10;
        phno=((rev*fact)+(phno%fact));
}
int main()
{
        phone p1[10];
        int cnt, i;
        cout<<"\n Enter No. of Elements : ";
        cin>>cnt;
        if(cnt<1 || cnt>10)
        {
                cout<<"\n Out of size";
                exit(0);
        }
        for(i=0; i<cnt; i++)
                p1[i].accept();
        cout<<"\n ------------------------------------------"; 
        cout<<"\n Original Phone Number : ";
        for(i=0; i<cnt; i++)
                p1[i].display();
        for(i=0; i<cnt; i++)
                p1[i].change();
        cout<<"\n Changed Phone Number  : ";
        for(i=0; i<cnt; i++)
                p1[i].display();
        return 0;
}
OUTPUTSCREEN:
Enter No. of Elements : 1                                                                                                       
                                                                                                                                 
 Enter STD Code        : 123                                                                                                     
                                                                                                                                 
 Enter Exchange Code   : 321                                                                                                     
                                                                                                                                 
 Enter Phone Number    : 6545                                                                                                    
                                                                                                                                 
 ------------------------------------------                                                                                      
 Original Phone Number :  123-321-6545                                                                                           
 Changed Phone Number  :  223-321-6545                                                                                           
                                                                                                                                 
...Program finished with exit code 0                                                                                             
Press ENTER to exit console.  
 

Potrebbero piacerti anche