EECS168 09:Lab13
| Navigation | 
|---|
| Home | 
| Information | 
| Classwork | 
Objectives
- Structures and Classes
 
Structures
A C++ struct (short for “structure”) can be thought of as a collection of related data items of varying types. Shown below is an example of a struct. The name StudentAccount is called the structure tag. The identifiers declared inside the braces are called member names. Remember to include the semicolon after the last brace when using structures. Like all identifiers in C++, struct data types must be declared before they are used. Once the structure type definition has been given (just before main in the example below), the structure type can be used just like any other predefined types such as int, char, and so forth. To gain access to each element of a structure, you can use a dot operator. An example of accessing a struct member is shown in the main() function. Write a program called account.cpp that uses the structure described below. The program should ask the user to enter the student name, the semester admitted to KU, major, and student number. The program will output the student information together with an expected graduation date (make the assumption that a Fall admission would result in a May graduation and a Spring admission will result in a December graduation). You don't need to check for validity of input.
struct StudentAccount
{
  string name;
  string major;
  int admission_year;
  string admission_semester;
  string expected_graduation_year;
  string expected_graduation_month;
};
int main()
{
   StudentAccount account;
   //Accessing the member variables
   cout<< "Please enter the following information: "<<endl;
   cout<< "Student Name: ";
   cin >> account.name;
   .
   .
   .
}
Classes
A class is a data type whose instances are objects. Which means that a class definition can have both member functions and member variables. A class is generally used to implement an Abstract Data Type (ADT: an encapsulation of data and operations on the data). While we generally use class instances when encapsulating methods with data and struct instances. When grouping together just data, the two constructs are in fact identical. (The only minor difference is that the default accessibility for members in a struct is “public” whereas in a class it is “private”). Use the following class definition for Cars to define two car objects. One of your cars should be a red Ford 2002. Add the private variable cost and appropriate member functions to get the cost (accessor function) and set the cost (mutator function). The red Ford should cost $6500.00. You must also add a member function to the Cars class that will print the color, make, year and cost. Print out the cost of the cars before the "big sale" and then reduce their price by some percentage obtained from the user. Print out the price after the reduction. Include your code in the file cars.cpp. Show the result of testing your program in a document called test.txt
class Cars
{
public:
  string get_color();
  string get_make();
  int number_of_gears();
  void set(int the_gears, string the_color, ... );
  // TO DO: ADD ANY OTHER FUNCTIONS
private:
  int gears;
  int year;
  string color;
  string make;
  //TO DO: ADD ANY OTHER PRIVATE VARIABLES
};
//----------------------
string Cars::get_color()
{
	string the_color;
	cout<< "What color is the car? ";
	cin>>the_color;
	return the_color;
}
//---------------------
void Cars::set(int the_gears, int the_year, ... )
{
   gears = the_gears;
   year = the_year;
   ...
}
//---------------------
int main()
{
        Cars car1;
	string make1 = car1.get_make(); 
	...
}
Lab Submission
- account.cpp
 - cars.cpp
 - test.txt