Handout 13

Structs and Classes


Struct Syntax

struct [ <tag> ] 
	{ <member-list> } 
    [ <declarators> ] ;

Struct Description

Use structs to group variables (<member-list>) into a single record.

<tag> - An optional tag name that refers to the structure type

<declarators> - The data item definitions, also optional.

Struct Example

struct my_struct { 
  char name[80], phone_number[80]; 
  int age, height; 
} my_friend; 

strcpy(my_friend.name, "Mr. Wizard"); // accessing an element 
my_friend.age = 37; // accessing another element 
struct my_struct my_friends[100]; // declaring additional instances of my_struct 

Another Struct Example (code)

// Struct Example
#include <iostream.h>
#include <string.h>

const int string_length = 20;

// struct definitions

struct employee {
	int ID;
	char name[string_length];
	char gender;
	char num_depend;
	float rate, tot_wages;
};

// function prototypes
void printIt(struct employee);

int main() {

	employee organist, musician;

	organist.ID = 1234;
	strcpy(organist.name, "Harry Palms");
	organist.gender = 'M';
	organist.num_depend = 0;
	organist.rate = 16.00;
	organist.tot_wages = organist.rate * 40.0F;

	cout << "The organist is ";
	
	switch(organist.gender) {
		case 'F': case 'f':
			cout << "Ms. ";
			break;
		case 'M': case 'm':
			cout << "Mr. ";
			break;
	}
    
	cout << organist.name << endl;

	// Struct assigment.

	musician = organist;  // Copy organist to musician

	printIt(musician);

	return 0;
}
void printIt(struct employee theEmployee) {

	cout << "Name: " << theEmployee.name << endl;
	cout << "Rate: " << theEmployee.rate << endl;
	cout << "Wage: " << theEmployee.tot_wages << endl;
}
/* output is:
The organist is Mr. Harry Palms
Name: Harry Palms
Rate: 16
Wage: 640
*/

Object Oriented Design

A technique for developing a prograrn in which the solution is expressed in terms of objects selfcontained entities composed of data and operations on that data.

Class Example

1    #include <iostream.h>
2    #include <math.h>
3
4    class Circle {
5    private:
6      float private_radius;
7    public:
8      Circle()                          // constructor, no arguments
9        {private_radius = 4.0;}
10     Circle(float x)               // constructor, one argument
11       {private_radius = x;}
12     void radius(float x) {     // define circle via radius fuhction
13       private_radius = x;
14     }
15     void diameter(float x) { // define circle via diameter function
16       private_radius = x / 2.0;
17     }
18     void circumference(float x) {  // define circle via circumference function
19       private_radius = x / (2 * M_PI);
20     }
21     void area(float x) {    // define circle via area function
22       private_radius = sqrt(x / M_PI);
23     }
24     void show_radius() {    // print radius function
25       cout << "radius = " << private_radius << endl;
26     }
27     void show_diameter() {     // print diameter function
28       cout << "diameter = " << (2.0 * private_radius) << endl;
29     }
30     void show_circumference() {     // print circumference function
31       cout << "circumference = " << (2.0 * private_radius * M_PI) << endl;
32     }
33     void show_area() {     // print area function
34       cout << "area = " << (private_radius * private_radius * M_PI) << endl;
35     }
36   };
37
38   int main() {
39
40     Circle a, b(4.0), c, d, e, f;
41
42     a.show_radius();           // display the circles radius
43     a.show_diameter();         // display the circles diameter
44     a.show_circumference();    // display the circles circumference
45     a.show_area();             // display the circles circumference
46
47     b.show_radius();           // display the circles radius
48     b.show_diameter();         // display the circles diameter
49     b.show_circumference();    // display the circles circumference
50     b.show_area();             // display the circles circumference
51
52     c.radius(4);               // define the circle by the radius
53     c.show_radius();           // display the circles radius
54     c.show_diameter();         // display the circles diameter
55     c.show_circumference();    // display the circles circumference
56     c.show_area();             // display the circles circumference
57
58     d.diameter(8);             // define the circle by the diameter
59     d.show_radius();           // display the circles radius
60     d.show_diameter();         // display the circles diameter
61     d.show_circumference();    // display the circles circumference
62     d.show_area();             // display the circles circumference
63
64     e.circumference(2*4*M_PI); // define the circle by the circumference
65     e.show_radius();           // display the circles radius
66     e.show_diameter();         // display the circles diameter
67     e.show_circumference();    // display the circles circumference
68     e.show_area();             // display the circles circumference
69
70     f.area(M_PI*4*4);          // define the circle by the area
71     f.show_radius();           // display the circles radius
72     f.show_diameter();         // display the circles diameter
73     f.show_circumference();    // display the circles circumference
74     f.show_area();             // display the circles circumference
75     
76     return 0;
77   }


source code