C++ Converting a Code to Classes and Objects

Hello,
I'm still fairly new to programming and I've come leaps and bounds from where I was just a couple months ago.
However, my Professor has been on leave and I can't seem to figure out Class and objects.

She has assigned a small and easy assignment without the use of C++11 and I'm frankly lost.
How would I begin to convert the original to use class and objects? She's been on leave and I've only gotten worksheets and little knowledge to the subject.
So, my questions are...
How does Class and objects work?
Where do I mark Parameters?
How would I insert Objects into an Array?
What would be your suggested root to solve this?
Please don't do the work for me as I wish to learn. I'm just looking for suggestions as well as a little nudge in the right direction.

The code below is The original (Ch 6 homework):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototype
double caltotalSales(double, double);
double calAverage(double, int);

int main()
{
	int years,				// The number of years 
		totalQuarters;		// The number of quarters
	double sales, 
		   totalSales = 0, // The total sales for all quarters
		   average;         // The average quarterly sales

	// Get the number of years.
	cout << "This program will calculate average sales over a \n"
		 << "period of years.  How many years do you wish to average? ";
	cin  >> years;

	// Validate the number of years.
	while (years < 1)
	{  cout << "Years must be at least one. Please re-enter: ";
		cin  >> years;
	}

	for (int yearNumber = 1; yearNumber <= years; yearNumber++) 
	{
		cout << "\nYear " << yearNumber << endl;
      
		// Get the sales amount for each quarter.
		for (int quarter = 1; quarter <= 4; quarter++) 
		{  
			cout << "sales amount for quarter "
				 << setw(2) << quarter << "? ";
			cin  >> sales;
         
			// Validate the sales amount.
			while (sales < 0)   
			{
				cout << "Sales amount must be zero or greater.\n";
				cout << "Sales amount for month  " 
					 << quarter << "? ";
				cin  >> sales;
			}
			 
         	// Get the total sales from funtion
			totalSales = caltotalSales(totalSales, sales);
		}	
		
	}
	
	
	// Calculate the total number of quarters.
	totalQuarters = years * 4;
   
	// Get the average from average function
   average = calAverage(totalSales, totalQuarters);
	// Calculate the average sales amount.


	// Display the total sales amount for the period.
		cout << fixed << showpoint << setprecision(2);
		cout << "\nOver a period of " << totalQuarters 
		 << " quarters, " << totalSales <<endl;
   
	// Display the average sales for the period.
	cout << fixed << showpoint << setprecision(2);
	cout << "Average quarterly sales for the period is " << average << endl;

	return 0;
}
	
//********************************************************
// The caltotalSales function calculates the total sales   *
// for a given years.                                   *
// ********************************************************

double caltotalSales(double totalSales, double sale)
{
	// Accumulate the sales amount.
	return totalSales += sale;  	
}	
   
//********************************************************
// The average function calculates the average sales   *
// for a given years.                                   *
// ********************************************************   
   	
double calAverage(double totalSales, int quarters)
{
	   
	   return totalSales / quarters;
}





She asks us to convert this program to use classes and Objects and Gave us this as reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Chapter 13, Programming Challenge 4: Personal Information Class
#include <iostream>
#include <string>
using namespace std;

// PersonalInfo class declaration
class PersonalInfo
{
private:
   string name;
   string address;
   int age;
   string phone;

public:
   // Default constructor
   // Sets all fields to null or 0.
   PersonalInfo()
   { name = "";
     address = "";
     age = 0;
     phone = "";
   }
   
   // Overloaded Constructor
   // Parameters: n is the name
   //   addr is the address
   //   a is the age
   //   p is the phone
   PersonalInfo(string n, string addr, int a, string p)
   {
      setName(n);
      setAddress(addr);
      setAge(a);
      setPhone(p);
   }
   
   // Mutator functions
   void setName(string n)
   { name = n; }

   void setAddress(string a)
   { address = a; }

   void setAge(int a)
   { age = a; }
   
   void setPhone(string p)
   { phone = p; }
   
   // Accessor functions
   string getName() const
   { return name; }
   
   string getAddress() const
   { return address; }
   
   int getAge() const
   { return age; }
   
   string getPhone() const
   { return phone; }
};

// Function prototye
void displayPersonalInfo(PersonalInfo);

// Demo program
int main()
{
   // Create the first instance of PersonalInfo.
   PersonalInfo me("Herb Dorfmann",
                   "27 Technology Drive",
                   25, "555-1212");
   
   // Create an instance for a family member.
   PersonalInfo auntSally("Sally Dorfmann",
                   "48 Friendly Street",
                   50, "555-8484");

   // Create an instance for a friend.
   PersonalInfo joe("Joe Looney",
                    "191 Apple Mountain Road",
                    30, "555-2222");
                   
   // Display the data in each object.
   displayPersonalInfo(me);
   displayPersonalInfo(auntSally);
   displayPersonalInfo(joe);
   
   return 0;
}

//**************************************
// The displayPersonalInfo function    *
// displays the data in a PersonalInfo *
// object.                             *
//**************************************
void displayPersonalInfo(PersonalInfo obj)
{
   cout << "Name: " << obj.getName()
        << endl;
   cout << "Address: " << obj.getAddress()
        << endl;
   cout << "Age: " << obj.getAge()
        << endl;
   cout << "Phone: " << obj.getPhone()
        << endl << endl;
}



I don't know how to format for this website so I apologize. EDIT: i think i figured it out. the formatting part/
I can't even begin to figure out how i would go about this question. this request.

If I'm misunderstanding it, here's the question word for word:
You will rewrite the program (Chapter 6 HW Solution.cpp) using class.

As you already know, Week 7 and 8 scenario are the same. For your information, I put Week 7 homework scenario again: Your company wants you to make a program to collect quarterly sales amount of a product and calculate the average sales over a period of years. In making the program, use nested loops. The program should first ask for the number of years. The outer loops will iterate once for each year. The inner loop will iterate four times, once for each quarter. Each iteration of the inner loop will ask the user for the sales amount for that quarter. After all iterations, the program should display the number of quarters, the total sales amount, and the average sales amount per quarter for the entire period.
Last edited on
First, consider following the tutorial sequence on this site, specifically these two:
http://www.cplusplus.com/doc/tutorial/structures/
http://www.cplusplus.com/doc/tutorial/classes/

Once you've done that, take a look at this post
http://www.cplusplus.com/forum/beginner/213266/#msg995536

The trickiest part of this assignment, IMO, is figuring out which concept most merits coalescing into a class.
So this brings forth a Question/Concept.
How would i determine the number of objects based on user input?


So i have a class.
and I bring forth the Objects like so.
How would I set it to where if the user inputs "6" ,for example, it would create 6 Objects?
1
2
3
class example;
class example 0;
class example 1;
Use a std::vector<example> to hold your n objects.
http://en.cppreference.com/w/cpp/container/vector#Example
Topic archived. No new replies allowed.