Question

Create a C++ console program that defines a class named EvenNumber that represents an even number....

Create a C++ console program that defines a class named EvenNumber that represents an even number. Create the class inside a separate header file (.h) and then include this header in your main source code file.

The class should have one private integer data field to store the even number. The default constructor should initialize the data field to 0. Also define a 1-arg constructor that initializes the object with the specified value.

Define a public getter method named getValue that should return the even number stored in the data field.

Define 2 public setter methods. setNext should set the data field to the next greater even number (+2) and setPrevious should set it to the next lesser even number (-2).

From main, prompt the user to input an even number, validate it, and then create an EvenNumber object supplying the user's value to the constructor. Use the object's methods and a loop(s) to display the 3 even numbers on both sides of the user's number.

input: "6"; output: "0 2 4 6 8 10 12"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Class file EvenNumber.h

class EvenNumber{
   private:
       int number;
   public:
       EvenNumber(){
           number=0;
       }
       EvenNumber(int num){
           number=num;
       }
       int getValue(){
           return number;
       }
       void setNext(){
           number+=2;
       }
       void setPrevious(){
           number-=2;
       }
      
};

/////////////////////////////////////////////////////////////////////////////////////////////////

//Source code file

#include<iostream>
#include "EvenNumber.h"

using namespace std;

int main(){
   int num;
   cout<<"Enter an even number: ";
   cin>>num;
   while(num%2!=0){
       cout<<"Please enter an even number: ";
        cin>>num;
   }
   EvenNumber even(num);
   even.setPrevious();
   even.setPrevious();
   even.setPrevious();
   for(int i=0;i<6;i++){
       cout<<even.getValue()<<" ";
       even.setNext();
   }
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Create a C++ console program that defines a class named EvenNumber that represents an even number....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create the header file named “Complex.h” that contains the following class: The class Complex represents a...

    Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...

  • PHP Create a class named Dwelling that includes properties for the number of rooms and the...

    PHP Create a class named Dwelling that includes properties for the number of rooms and the size of the plot. Add getter and setter methods. The constructor requires no arguments and automatically sets the number of rooms to 2 and the plot size to 100.

  • IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...

    IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number,...

    a. Create a class named Lease with fields that hold an apartment tenant's name, apartment Number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name. "XXX", the apartment number to O, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy...

  • In Java For the following questions, “define a class” means the following: • Create an appropriately-named...

    In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • c ++ Create a class Book with the data members listed below.     title, which is...

    c ++ Create a class Book with the data members listed below.     title, which is a string (initialize to empty string)     sales, which is a floating point value (as a double, initialize to 0.0) Include the following member functions:     Include a default constructor,     a constructor with parameters for each data member (in the order given above),     getters and setter methods for each data member named in camel-case. For example, if a class had a data...

  • CIST 2371 Introduction to Java Unit 04 Lab Due Date: ________ Create a folder called Unit04...

    CIST 2371 Introduction to Java Unit 04 Lab Due Date: ________ Create a folder called Unit04 and put all your source files in this folder. You will create a UML diagram and two Java class files and that implements the classes in the UML diagram. Diagram Create a UML diagram that shows an Account class and an AccountTester class. The Account class holds the following data as Strings: account Number, type of account, card number and expire date. Include getter...

  • 2. Create a class named Student that contains the following » idStudent. The idStudent is an...

    2. Create a class named Student that contains the following » idStudent. The idStudent is an int variable that holds the student's matric number. name. The name field references a String object holds the student's name. . major. The major field references a String object holds the student's major. . classification. The classification field references a String object holds the student's classification level (bongsu, kecil, muda, sulung) . Constructor should accept the student's matric number, name, major and classification as...

  • Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...

    Write a C++ console program that defines a class named Course that utilizes a dynamically allocated array. Do not use the vector class for this assignment. The Course class should define private data members for the name of the course, the number of students in the course, an array of student names (string*), and the capacity of the course (the array may not be full of students). Use pointer notation when dealing with the array. A 2-arg constructor should initialize...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT