Question

C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the...

C++

Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the following attributes:

Data members (private):

string: username

int array: ratings

Number of elements should be size

int: numRatings

Number of books in the database

Int: size

The capacity of the ratings array (50). Constant

Member functions (public):

Default constructor

Sets username to an empty string, numRatings to 0,

size to 50, and all the elements of ratings array to the value 0

Parameterized constructor

Takes a string, an array of integers, and one integer for initializing username, ratings, and numRatings, respectively. Make sure the value passed into the constructor for numRatings does not exceed the value of the data member size

getUsername()

Returns username as a string

setUsername(string)

(void) Assigns username the value of the input string

getRatingAt(int)

Parameter: int index. Returns the rating stored at the specified index. If index is larger than the size of the ratings array, returns -1.

setRatingAt(int,int)

Parameters: int index, int value. Sets the rating to value at the specified index, if index is within the bounds of the array and value is between 0 and 5. Returns a boolean, true if the rating is successfully updated and false otherwise.

getNumRatings()

Returns numRatings

setNumRatings(int)

(void) Assigns numRatings the value of the input int

getSize()

Returns size

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

If you have any doubts, please give me comment...

User.h

#ifndef USER_H

#define USER_H

#include <string>

using namespace std;

class User{

private:

string username;

int ratings[50];

int numRatings;

int size;

public:

User();

User(string uname, int _ratings[], int n_ratings);

string getUsername();

void setUsername(string uname);

int getRatingAt(int ind);

bool setRatingAt(int ind, int val);

int getNumRatings();

void setNumRatings(int val);

int getSize();

~User();

};

#endif

User.cpp

#include "User.h"

User::User(){

username = "";

numRatings = 0;

size = 50;

for(int i=0; i<size; i++)

ratings[i] = 0;

}

User::User(string uname, int _ratings[], int n_ratings){

username = uname;

size = 50;

numRatings = n_ratings;

for(int i=0; i<n_ratings; i++)

ratings[i] = _ratings[i];

}

string User::getUsername(){

return username;

}

void User::setUsername(string uname){

username = uname;

}

int User::getRatingAt(int ind){

if(ind<numRatings)

return ratings[ind];

return -1;

}

bool User::setRatingAt(int ind, int val){

if(ind<size){

ratings[ind] = val;

return true;

}

return false;

}

int User::getNumRatings(){

return numRatings;

}

void User::setNumRatings(int val){

numRatings = val;

}

int User::getSize(){

return size;

}

User::~User(){

}

Add a comment
Answer #2

Program:

User.h

User.cpp

-------------------------------------------------------------------------------

Output:

--------------------------------------------------------------------

Program:

User.h

#include<string>

using namespace std;

class User
{
   private:
       //Declare the member variables.
       string username;
       int ratings[50];
       int numRatings;
       int size;

   public:
       //Define the member functions.
       User();
       User(string name, int[], int);
       string getUsername();
       void setUsername(string);
       int getRatingAt(int);
       bool setRatingAt(int,int);
       int getNumRatings();
       void setNumRatings(int);
       int getSize();
};

User.cpp

// ConsoleApplication41.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
#include "User.h"
using namespace std;

   //Default constructor
   User::User()
   {
       username = "";
       numRatings = 0;
       size = 50;

       for (int i = 0; i < size; i++)
           ratings[i] = 0;
   }

   //Parameterized constructor
   User::User(string name, int arr[], int rating)
   {
       username = name;

       if (rating <= size)
           numRatings = rating;
       else
           numRatings = 0;

       for (int i = 0; i < numRatings; i++)
           ratings[i] = arr[i];
   }

   //Returns the username.
   string User::getUsername()
   {
       return username;
   }

   //Sets the username.
   void User::setUsername(string name)
   {
       username = name;
   }

   //Returns the rating at a given index.
   int User::getRatingAt(int index)
   {
       if (index > size)
           return -1;

       return ratings[index];
   };

   //Sets the rating at a given index.
   bool User::setRatingAt(int index, int value)
   {
       if (index <= 0 || index > size)
           return false;

       else
           ratings[index] = value;
   }

   //Returns the variable numRatings.
   int User::getNumRatings()
   {
       return numRatings;
   }

   //Sets the value of the variable numRatings.
   void User::setNumRatings(int nr)
   {
       numRatings = nr;
   }

   //Returns the variable size.
   int User::getSize()
   {
       return size;
   }

   //Driver function.
int main()
{
   User user;
   user.setNumRatings(5);
   user.setRatingAt(1, 5);
   user.setRatingAt(2, 7);
   user.setRatingAt(3, 9);
   user.setRatingAt(4, 4);
   user.setRatingAt(5, 8);

   cout << "The rating for index 2 is:" << user.getRatingAt(2)<<endl;

   system("pause");
  
}

Add a comment
Know the answer?
Add Answer to:
C++ Create a User class, with a separate interface (User.h) and implementation (User.cpp), comprised of the...
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
  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test...

    USE JAVA: Given the Interface Code and the Interface Implementation Code; Write Junit Tests to test all fuctionality. ------------- Interface code: public interface CustomList { /** * This method should add a new item into the CustomList and should * return true if it was successfully able to insert an item. * @param item the item to be added to the CustomList * @return true if item was successfully added, false if the item was not successfully added (note: it...

  • COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see...

    COSC 1437 C++2 Project Assignment 3 Description: Computer Science Department is evaluating its professors to see which professor has the highest rating according to student input. You will create a ProfessorRating class consisting of professor Name and three ratings. The three ratings are used to evaluate easiness, helpfulness, and clarity. The value for each rating is in the range of 1 to 5, with 1 being the lowest and 5 being the highest. Your program should contain the following functionalities:...

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Using Java write a program that performs the following: 1. Define a class that contains an...

    Using Java write a program that performs the following: 1. Define a class that contains an array of type int as a data member. 2. Define a constructor that creates an array of 1024 elements and assigns it to the class data member. The constructor shall populate the elements with random numbers ranging in value from 1 to 1024. 3. Define and implement a search() method that take a parameter of type int and returns the index location if the...

  • In C++, create a class, called DynamicCharArray, which wraps around the standard char array and offers...

    In C++, create a class, called DynamicCharArray, which wraps around the standard char array and offers the following features: • Default constructor: initializes an internal array of 8 elements • Copy constructor: called implicitly when making a copy • int size(): returns the length of the array • void expand(int amount): increases the capacity of the array by the specified amount. It will need to create a new internal array and copy the elements over to accomplish this correctly, but...

  • Create a LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

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