Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Needs to be written in C++ doesnt need to compile just snippets ----------------

ID: 3697728 • Letter: N

Question

Needs to be written in C++

doesnt need to compile just snippets

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

1). Explain what public: and private: mean in a class definition.

2). Suppose your program contains the following class definition,

class Car

{

public:

void setPrice(double newPrice);

void setProfit(double newProfit);

double getPrice( );

private:

double price;

double profit;

double getProfit( );

};

and suppose the main function of your program contains the following declaration and that the program somehow sets the values of all the member variables to some values:

Car ford, toyota;

Which of the following statements are then allowed in the main function of your program?

ford.price = 4999.99;

toyota.setPrice(30000.97);

double aPrice, aProfit;

aPrice = toyota.getPrice( );

aProfit = toyota.getProfit( );

aProfit = ford.getProfit( );

ford = toyota;

3). Suppose you change Self-Test Exercise 2 so that in the definition of the class Car all member variables are public instead of private. How would this change your answers?

4). Define a class for a type called Counter. An object of this type is used to count things, recording a count that is a non-negative whole number. Include a mutator function that sets the counter to a count given as an argument. Include member functions to increase the count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become negative. Also, include a member function that returns the current count value and one that outputs the count.

5). Create a class named Gizmo that includes SKU number (integer) and a price (double). It also contains a data member that holds a “serial number” for each object created from the class. That is, the first object created will be numbered 1, the second 2, and so on.

Explanation / Answer

1)public and private are access specifiers.They modify the access rights for the members of the class.
public: these members are accessible from anhywhere where the object is visible.
private:these members are accessible only from within the other members of the class.

2)toyota.setPrice(30000.97);
double aPrice, aProfit;
aPrice = toyota.getPrice( );
ford = toyota;

3) ford.price = 4999.99;
toyota.setPrice(30000.97);
double aPrice, aProfit;
aPrice = toyota.getPrice( );
ford = toyota;

4)

#include <iostream>
using namespace std;

class Counter
{
private :
int count;
public:
void setCount(int c)
{
      count=c;
}
void incCount()
{
      count++;
}
void decCount()
{
      if(count==1)
      cout<<"Counter can't be decreased. Already on the threshold value";
      else
      count--;
}
int getCount()
{
      return count;
}
void display()
{
      cout<<"Counter value : "<<getCount();
}


};
int main()
{
   cout << "****Counter Example**** "<< endl;
   Counter c;
   c.setCount(21);
   c.display();
   c.decCount();
   c.decCount();
   c.decCount();
   c.incCount();
   c.display();
   return 0;
}

5)

#include <iostream>
using namespace std;
class Gizmo
{
static int count;
int sNo;
int SKU;
string name;

public:
Gizmo(int sku,string n)
{
      SKU=sku;
      name=n;
      sNo=++count;
}
void display() const
{
      cout<<sNo<<" "<<name<<" "<<SKU;
}

};
int Gizmo::count=0;
int main()
{
   cout<<"Gizmo Details:"<<endl;
   Gizmo g1(123,"bac");
   g1.display();
   cout<<endl;
   Gizmo g2(456,"def");
   g2.display();

   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote