answersLogoWhite

0

The following example code demonstrates unary increment operator overloads. The code makes use of two classes, natural and prime, where prime inherits from natural, but where both specialise their increment operators. Note that natural numbers can never be zero, therefore if an increment causes an overflow, the number is set to 1. Prime numbers are also natural numbers but they must be greater than 1. The main function tests both the prefix and postfix increment operators for both classes.

#include<iostream>

class natural

{

private:

unsigned m_data;

public:

natural(unsigned data=1): m_data(data?data:1) {}

// prefix increment operator

natural& operator++() {

++m_data;

if(!m_data)

m_data=1;

return(*this);

}

// postfix increment operator

natural operator++(int) {

natural temp(*this);

++m_data;

if(!m_data)

m_data=1;

return(temp);

}

// conversion operator

operator unsigned(){

return(m_data); }

};

std::ostream& operator<<(std::ostream& os, natural& num)

{

os<<(unsigned)num;

return(os);

}

class prime: public natural

{

private:

bool is_prime()

{

unsigned num = (unsigned)*this;

if(num==1)

return(false);

if(num==2)

return(true);

if(num%2==0)

return(false);

unsigned max_factor = (unsigned)sqrt((double) num);

for(unsigned factor=3; factor<max_factor; factor+=2)

if(num%factor==0)

return(false);

return(true);

}

public:

prime(): natural(2) {}

prime& operator++() {

do

{

natural::operator++();

} while(!is_prime());

return(*this);

}

prime operator++(int) {

prime temp(*this);

do

{

natural::operator++();

} while(!is_prime());

return(temp);

}

};

int main()

{

natural n;

prime p;

std::cout<<"The 1st natural number is "<<n<<std::endl;

std::cout<<"The 2nd natural number is "<<++n<<std::endl;

std::cout<<"The 3rd natural number is "<<++n<<std::endl;

std::cout<<"The 4th natural number is "<<++n<<std::endl;

std::cout<<"The 4th natural number was "<<n++<<" while the 5th is "<<n<<std::endl;

std::cout<<"The 1st Prime number is "<<p<<std::endl;

std::cout<<"The 2nd prime number is "<<++p<<std::endl;

std::cout<<"The 3rd prime number is "<<++p<<std::endl;

std::cout<<"The 4th prime number is "<<++p<<std::endl;

std::cout<<"The 4th prime number was "<<p++<<" while the 5th is "<<p<<std::endl;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

How can you create a new operator through operator overloading?

You cannot create a new operator through operator overloading. You can only redefine an existing operator, with certain limitations. As an example, for a class of complex numbers, having a real and an imaginary part, you might want an addition operator. This is the skeleton of code to do that. I only show the operator, not any constructors or other operators or methods, etc.class complex {private:double real, imaginary;public:complex operator+ (complex operand) {complex temp;temp.real = this.real + operand.real;temp.imaginary = this.imaginary + operand.imaginary;return temp;}};The above answer is for C++. Since this question is also categorized in Java Programming it's important to note that operator overloading is not currently possible in Java.


Why do my two different calculators both show minus three squared as negative nine instead of positive?

The minus (unary) operator is of lower precedence than the exponent. To force the square of a negative on some calculators you may need to surround the number in parentheses. E.g.


Who was the operator on the Andy Griffith Show?

I believe that Andy Griffith himself came up with the concept for the show. However, I might be wrong about that. I do know that he had a large influence over what went on, though; he always insisted that the musicians on the show actually play, instead of having actors pretend to play.


How can we show that the position operator is Hermitian?

To show that the position operator is Hermitian, we need to demonstrate that its adjoint is equal to itself. In mathematical terms, this means proving that the integral of the complex conjugate of the wave function multiplied by the position operator is equal to the integral of the wave function multiplied by the adjoint of the position operator. This property is essential in quantum mechanics as it ensures that the operator corresponds to a physical observable.


Write a program to write the sum of two complex number using friend function and overloading constractor?

#include&lt;iostream.h&gt; #include&lt;conio.h&gt; class complex { int r; int i; public: complex() { } complex(int a,int b) { r=a;i=b; } friend complex operator+(complex,complex); friend show(complex); complex operator+(complex c1,complex c2) { complex c3; c3.r=c1.r+c2.r; c3.i=c1.i+c2.i; return(c3); } show(complex c) { cout&lt;&lt;c.r&lt;&lt;"i+"&lt;&lt;c.i&lt;&lt;endl; } void main() { complex a,b,c; clrscr(); a.complex(3,6); b.complex(4,7); c=a+b; show(a); show(b); show(c); getch() }


What was the name of the telephone operator on the show Green Acres?

Her name was Sarah.


How much do owner operator trucking jobs pay?

The website http://www.owneroperatorjob.com/ will show you a lot of information about operator trucking jobs. It will show you a listing of jobs by State along with the pay and a lot of other information about the job itself.


Do you need to show a passport on a coach from dublin to London?

No, but double check with the coach operator.


What was the concept of the show Doctor Who?

It is all science-fiction


What are the release dates for Amber's Show - 2013 The Concept 1-1?

Amber's Show - 2013 The Concept 1-1 was released on: USA: 23 January 2013


How do snakes show love?

They don't ! To show love - you have to understand what emotions are. Reptiles have no concept of the meaning of emotions.


Was Gilda Radner the telephone operator on Saturday Night Live?

No, Gilda Radner was not the telephone operator on Saturday Night Live. She was one of the original cast members and is best known for her iconic characters like Roseanne Roseannadanna and Emily Litella. The telephone operator character, played by Laraine Newman, was part of a recurring sketch on the show. Radner's contributions were pivotal in defining the show's early success.