Question

In: Computer Science

Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all...

Suppose that class OrderList has a private attribute double cost[100] which hold the cost of all ordered items, and a private attributes int num_of_items which hold the number of items ordered. For example, if num_of_items is 5, then cost[0], cost[1], ..., cost[4] hold the cost of these 5 items. Implement the member function named total_cost which returns the total cost of this OrderList.

Solutions

Expert Solution

In the c++ code below we have a class called OrderList that has two data members- cost[] that stores cost of each item and n_of_items that stores number of items.

And then we have a member function total_cost() that calculates and returns the total cost of the items.

First, it asks user to enter the number of items and then runs a for loop that number of times to gat the cost of each item from the user. Simultaneously, it sums up the costs and finally returns the sum.

CODE:

#include<iostream>

using namespace std;

class OrderList

{

private:

double cost[100];

int num_of_items;

public:

double total_cost() //defining the member function

{

int i;

double sum = 0; //to get the total cost

cout<<"Enter the number of items: ";

cin>>num_of_items;

cout<<"Now enter cost of each item:\n";

for(i=0;i<num_of_items;i++)

{

cin>>cost[i];

sum +=cost[i]; //adding each cost to the sum

}

return sum; //returning the sum

}

};

int main()

{

OrderList o; //creatiing object of the class

double sum;

sum = o.total_cost(); //calling the member function

cout<<"The total cost is: "<<sum;

return 0;

}

OUTPUT:


Related Solutions

Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount)...
Suppose that Account class has private attributes double balance and two public methods void setBalance(double amount) and double getBalance() const. The method names explain its purpose. Further suppose that child class Savings has one more attribute double interest_rate and a public method void addInterest() which will update the balance according the formula new balance = old balance * (1 + interest_rate/100.0); Implement addInterest method. c++
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
A incomplete definition of a class Temperature is given below: public class Temperature { private double...
A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3}; } [6] (i) Copy and put it in a new class. Write a method toString() of the class, which does not have any parameters and returns a string containing all the values separated by newlines. When the string is printed, each value should appear on a line in the ascending order of their indexes. Copy the content of...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
The Account class Create a class named Account, which has the following private properties:
in java The Account class Create a class named Account, which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNumber (), getBalance (), setBalan newBalance). There is no setNumber () once an account is created, its account number cannot change. Now implement these methods: void deposit (double amount) and void withdraw (double amount). For both these methods, if the amount is less than...
The Account class Create a class named Account , which has the following private properties:
 The Account class Create a class named Account , which has the following private properties: number: long balance: double Create a no-argument constructor that sets the number and balance to zero. Create a two-parameter constructor that takes an account number and balance. First, implement getters and setters: getNunber(), getBalance(), setBalance (double newBalance) . There is no setNunber() - once an account is created, its account number cannot change. Now implement these methods: void deposit (double anount) and void withdraw(double anount). For both these methods, if the amount is less than zero,...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second;...
Consider the following statements: #include #include class Temporary { private: string description; double first; double second; public: Temporary(string = "", double = 0.0, double = 0.0); void set(string, double, double); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); }; Write the definition of the member function set() so that the instance variables are set according to the parameters. Write the definition of the constructor so that it initializes the instance variables using the function set() Write...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function...
(C++ programming) Assignment *Circle Class -Radius r (private) as an attribute variable -Member function -Get(): Function that returns r value of property variable -Put(int d): A function that stores d in attribute variable r *Declare a one-dimensional array of type Circle and in each array element Read and store integers from standard input device *Declare the swap() function to swap two elements with each other *Write a program that sorts the elements of a one-dimensional array of circle type in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT