Notes on Section 10.2

Terms

  • Class

  • Object

  • Public Members (Variables and Functions)

  • Private Members (Variables and Functions)

  • Scope Resolution Operator

  • Accessor Functions, Mutator Functions

  • Constructor

  • Default Constructor

Tips

  • Define classes globally, but declare them locally.

  • DO NOT forget the semicolon after the class definition. It's very easy to do.

  • You may use the same name for a member variable in different classes.

  • You may assign one class's value to another class. This has the effect of assigning all member variables one-by-one. (So if I had two classes of type StudentRecord , call them stu_1 and stu_2, and I used the assignment command stu_1=stu_2, this would have the effect of assigning stu_1.id_number the value that is in stu_2.id_number, assigning stu_1.units the value that is in stu_2.units, etc.

  • A function may return a class, just as functions can return integers, characters, etc.

  • A class may have another class or a structure as a member variable. If I wanted to define a class KenStudent , I might have a member variable of type char for gender ('F' or 'M'), a member variable string for the name, and a member variable of type AcadRecord for the student's academic record, for example.

  • Scope resolution quantifiers are used when defining the functions of a class. When calling the functions, use the dot operator, just as we have been.

  • When defining functions for classes, you don't need to specify which object's members are being dealt with. If we are defining a function for a class which has a member variable "size", I don't need to use a dot operator when I want to talk about that variable size. It is assumed that the size variable I am talking about is the one from the object whose member function this function is to be.

  • If you don't specify whether member variables/functions are public or private, they will be private.

  • You can switch back and forth between declaring public and private variables and functions as often as you would like to in one class definition, but it is conventional to declare all public things first, then all private things.

  • It is good programming practice to make all member variables private, using accessor and mutator functions as necessary.

  • Try to define your classes so that someone who wants to use your class in a program doesn't have to worry about the details of how it is implemented. Someone who wants to use your class should only need to know how the various public functions operate. (A class so defined is called an "Abstract Data Type".)

Syntax

class AcadRecord    Class definition
{
 public:		The following members are public:
 void init(); //Sets all values to 0;
   void set_id(int new_id); // Sets the id number;
   void update(double curr_units, double curr_gpa); //Updates a record's count of units and</sticky> <sticky>                                                    //semesters; adjusts numbers for gpa as well.
   void output(); // Outputs all relevant data.	

   int get_id();
   double get_units();  //These four are
   double get_gpa();    //accessor functions.
   int get_semesters();
 The above are function declarations -- definitions with all other function definitions.

 private:
   void incr_sem(); //Increments semester Private function
  int id_number;			The rest are private variables
  double units;
  double grade_points;
  int semesters;
};

void AcadRecord::update(double curr_units, double curr_gpa)
// This is the definition of a function which is a member function of the class AcadRecord. 
// The :: indicates this. It would be called using a dot operator. 
// This would appear at the end of the program with all of the other function definitions.)

{
 grade_points=grade_points+(curr_units+curr_gpa);
 units = units + curr_units;
 incr_sem();
}


double AcadRecord::get_gpa()
{
 return grade_points/units;
}
Topic revision: r1 - 2015-11-01 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback