Assignment Ofc++3rd

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

Q1.Design a class Time to represent time in hours,minutes and seconds.

Overload the
appropriate set of operators for performing I/O of time and manipulating objects of
Time class.

ANS.

#INCLUDE<IOSTREAM.H>

#INCLUDE<CONIO.H>

Class time;

Int hrs;

Int min;

Public:

Time(int t)

Hour=t/60;

Mins=t%60*60;

};

Q2. How is it possible to distinguish between prefix and postfix notation while
overloading increment ‘ ++’ and decrement ‘--‘ operators ?

Overloading the increment (++) and decrement (--) operators are pretty straightforward,
with one small exception. There are actually two versions of the increment and
decrement operators: a prefix increment and decrement (e.g. ++nX; --nY ;) and a postfix
increment and decrement (eg. nX++; nY--;).

Because the increment and decrement operators modify their operands, they’re best
overloaded as member functions. We’ll tackle the prefix versions first because they’re
the most straightforward.
Overloading prefix increment and decrement

Prefix increment and decrement is overloaded exactly the same as any normal unary
operator. We’ll do this one by example:

class Digit   

{   

private:   

    int m_nDigit;   

public:   

    Digit(int nDigit=0)   

    {   

        m_nDigit = nDigit;   

    }   

    Digit& operator++();  

    Digit& operator--();  

      Int GetDigit() const { return m_nDigit; }   

};   

Digit& Digit::operator++()

{       // If our number is already at 9, wrap around to 0   

    If (m_nDigit == 9)   

        m_nDigit = 0;   

    // otherwise just increment to next number

    else  
      ++m_nDigit;     

    return *this;   

}   

  

Digit& Digit::operator--()   

{   

    // If our number is already at 0, wrap around to 9   

    If (m_nDigit == 0)   

        m_nDigit = 9;   

    // otherwise just decrement to next number   

    else  

        --m_nDigit;   

  

    return *this;   

}  

class Digit

private:

int m_nDigit;

public:

Digit(int nDigit=0)

{
m_nDigit = nDigit;

Digit& operator++();

Digit& operator--();

int GetDigit() const { return m_nDigit; }

};

Digit& Digit::operator++()

// If our number is already at 9, wrap around to 0

if (m_nDigit == 9)

m_nDigit = 0;

// otherwise just increment to next number

else

++m_nDigit;

return *this;

Digit& Digit::operator--()

// If our number is already at 0, wrap around to 9

if (m_nDigit == 0)

m_nDigit = 9;

// otherwise just decrement to next number


else

--m_nDigit;

return *this;

Our Digit class holds a number between 0 and 9. We’ve overloaded increment and
decrement so they increment/decrement the digit, wrapping around if the digit is
incremented/decremented out range.

Note that we return *this. The overloaded increment and decrement operators return a
Digit so multiple operators can be “chained” together. Consequently, we need to return
an item of type Digit. Since these operators are implemented as member functions, we
can just return *this, which is an item of type Digit!

Overloading postfix increment and decrement

Normally, functions can be overloaded when they have the same name but a different
number and/or different type of parameters. However, consider the case of the prefix
and postfix increment and decrement operators. Both have the same name (eg.
operator++), are unary, and take one parameter of the same type. So how it is possible
to differentiate the two when overloading?

The answer is that C++ uses a “dummy variable” or “dummy argument” for the postfix
operators. This argument is a fake integer parameter that only serves to distinguish the
postfix version of increment/decrement from the prefix version. Here is the above Digit
class with both prefix and postfix overloads:

Digit& Digit::operator++()   

{   

    // If our number is already at 9, wrap around to 0   

    if (m_nDigit == 9)   

        m_nDigit = 0;   

    // otherwise just increment to next number   

    else  
        ++m_nDigit;   

  

    return *this;   

}   

  

Digit& Digit::operator--()  

{       // If our number is already at 0, wrap around to 9   

    if (m_nDigit == 0)

  

        m_nDigit = 9;   

    // otherwise just decrement to next number   

    Else

  

        --m_nDigit;

 return *this;   

we’ve distinguished the prefix from the postfix operators by providing an integer dummy
parameter on the postfix version. Second, because the dummy parameter is not used in
the function implementation, we have not even given it a name. This tells the compiler
to treat this variable as a placeholder, which means it won’t warn us that we declared a
variable but never used it.

the prefix and postfix operators do the same job — they both increment or decrement
the class. The difference between the two is in the value they return. The overloaded
prefix operators return the class after it has been incremented or decremented.
Consequently, overloading these is fairly straightforward. We simply increment or
decrement our member variables, and then return *this.

The postfix operators, on the other hand, need to return the state of the class before it is
incremented or decremented. This leads to a bit of a conundrum — if we increment or
decrement the class, we won’t be able to return the state of the class before it was
incremented or decremented. On the other hand, if we return the state of the class
before we increment or decrement it, the increment or decrement will never be called.

Q3.How can a function be used in more than one forms? Give example with explanation.

ANS.

#INCLUDE<IOSTREAM.H>

Int main()

Cout<<volume(10)<<”/n”;

Cout<<volume(2.5,8)<<”/n;

Cout<<volume(100l,75,15)<<”/n”;

Return o;

Int volume(int s)

Return(s*s*s);

Double volume(doubler,int h)

Return(3.14519*r*h);

}
Long volume (longl,int b,int h)

Return(l*b*h);

Output-:

1000

157.26

112500

Overloading of the functions should be done with caution.we should not overload
unrelated functions and should reserve function overloading for functions that
perform closely related tasks.sometime, the default argument may be used instead
of functions are extensively used for handling class objects.

Q4.How do overloading and overriding differ. Explain with practical example and
explanation.

ANS.

Overriding- means inheriting the function ( eg from base to derived ) and use the
function per the functionality we require in the derived class

overloading is using the same function name for 'n' different functions  with different no of
parameters or diffrent type of parameters in same class for performing the required functionality

In C++, overriding is a concept used in inheritance which involves a base class


implementation of a method. Then in a subclass, you would make another
implementation of the method. This is overriding. Here is a simple example.

class Base
{
public:
virtual void DoSomething() {x = x + 5;}
private:
int x;
};
class Derived : public Base
{
public:
virtual void DoSomething() { y = y + 5; Base::DoSomething(); }
private:
int y;
};

Here you can see that the derived class overrides the base class method DoSomething to
have its own implementation where it adds to its variable, and then invokes the parent
version of the function by calling Base::DoSomething() so that the x variable gets
incremented as well. The virtual keyword is used to that the class variable can figure out
which version of the method to call at runtime.

Overloading is when you make multiple versions of a function. The compiler figures out
which function to call by either 1) The different parameters the function takes, or 2) the
return type of the function. If you use the same function declaration, then you will get a
compiler error because it will not know which function to use. Here is another simple
example.

class SomeClass
{
public:
void SomeFunction(int &x) { x *= x; }
int SomeFunction(int x) { return x * x; }
};

// In main()
SomeClass s;
int x = 5;
x = SomeFunction(x);

The compiler knows to call the second implementation of the method because we are
assigning the return value from the function to x.

You might also like