CS 1440 Lab 7

Cover sheetPrevious ActivityNext Activity


Activity 7-2: C++ class data type

Activity Goals

        Learn how to define a C++ class

        Understand the public and private access words

Activity Procedure

As the prelab said, C++ classes are more powerful and versatile than structs.  In addition to data members, classes can have function members and can control who has access to members.  First, let’s talk about the access reserved words: “public” and “private.”

You may be familiar with the term “client” from using the web.  In the context of the web, a client is a web browser you are using to access information on some web server.  In the context of a C++ program, a client of a struct or a class is any function that declares variables of the struct or class.

 

If a member of a class is granted “public” access then the client can use that member in expressions.  (So, the members of structs must be public!).  If a member of a class has “private” access then the client can NOT use that member in expressions.  Look at the following and make sure you understand WHY (in terms of public/private access) some member access is okay and some is not allowed.

 

#include  <iostream>

#include  <string>

 

class  Student

{

public:

     string  name;     /* clients can use */

     string  grade;    /* clients can use */

 

private:

     double  test1, test2, test3;  /* clients CANNOT use */

     double  average;              /* clients CANNOT use */

};

 

int main(void)

{

     Student  s;

 

     s.name = "Jane Doe";    /* Okay */

 

     s.test1 = 34.0;         /* NOT ALLOWED */

}

 

 

Function Members

Classes can also have function members (unlike structs).  The access to function members is also controlled by the public and private reserved words.  Thus, clients can NOT call private function members.  Here is a similar Student class with some function members.

 

#include  <iostream>

#include  <string>

 

class  Student

{

private:

     string  name;     /* clients CANNOT use */

     string  grade;    /* clients CANNOT use */

     double  test1, test2, test3;  /* clients CANNOT use */

     double  average;              /* clients CANNOT use */

 

     void  print_average();

 

public:

     void  get_info();

     void  print_info();

};

 

int main(void)

{

     Student  s;

 

     s.name = "Jane Doe";    /* NOT ALLOWED */

     s.get_info();           /* Okay */

     s.print_average();      /* NOT ALLOWED */

     s.print_info();         /* Okay */

}

 

 

The function members are implemented as follows (notice the class name is repeated and the double colon “scope resolution” operator is used):

 

void  Student::get_info() 
{
     cout  <<  "Enter student’s last name: ";
     cin >> name;          /* WHOSE DATA MEMBER IS THIS? */
     cout  <<  "Enter 3 test scores: ";
     cin >> test1 >> test2 >> test3;  /* WHOSE DATA MEMBERS? */
     average = (test1 + test2 + test3) / 3;   /* WHOSE DATA MEMBERS? */
     cout  <<  "Enter final letter grade: ";
     cin >> grade;     /* WHOSE DATA MEMBER?? */
}

void  Student::print_info()
{
     cout  <<  "Student: "  <<  name  <<  endl;
     cout  <<  "\tGrade: "  <<  grade  <<  endl;
     cout  <<  "\tTests: "  <<  endl;
     cout  <<  "\t\tAverage: ";
     print_average();
     cout  <<  endl;
     cout  <<  "\t\tScores: "
           <<  test1  <<  ","
           <<  test2  <<  ","
           <<  test3  << endl;
}

void  Student::print_average()
{
     cout  <<  average;
}

Look back over the code above.  Notice how main( ) as a client of Student “s” can NOT call the member function print_average( ), but it is okay to call print_average from within the print_info function.  Private data and function members ARE accessible from within other class functions (private or public).  This is because member functions are not clients.  They are member functions!  You can’t be a client to yourself. 

Activity Followup

Cut and paste the code above into a file named  class.C  in your Lab7 directory.  Try to compile the file.  Notice how the compiler informs you that a client has illegally attempted to access private members.  Comment out or delete these illegal accesses and compile it again.

Look your code over carefully.  Try to understand it, ask your instructor, lab assistant, and/or neighbor classmate for help if you need it.