CSI 221 Programming 2 1 Defining the Class class smallobj //define a class { private: int somedata; //class data public: void setdata(int d) //member function to set data { somedata = d; } void showdata() //member function to display data { cout << “Data is “ << somedata << endl; } }; 2 int main() { smallobj s1, s2; //define two objects of class smallobj s1.setdata(1066); //call member function to set data s2.setdata(1776); s1.showdata(); //call member function to display data s2.showdata(); return 0; } 3 4 5 6 7 8 9 OUTPUT: 10 11 12 13 14 15 16 17 Destructors • A destructor function is called automatically when an object is destroyed. • A destructor has the same name as the constructor (which is the same as the class name) but is preceded by a tilde ‘~’: class Foo { private: int data; public: Foo() : data(0) //constructor (same name as class) }{ ~Foo() //destructor (same name with tilde) }{ }; 18 Objects as Function Arguments #include <iostream> using namespace std; //////////////////////////////////////////////////////////////// class Distance //English Distance class { private: int feet; float inches; public: //overload constructor (no args) Distance() : feet(0), inches(0.0) } { //constructor (two args) Distance(int ft, float in) : feet(ft), inches(in) } { void getdist() //get length from user { cout << “\nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void showdist() //display distance { cout << feet << “\’ -” << inches << ‘ \” ’; } void add_dist( Distance, Distance ); }; //declaration 20 void Distance :: add_dist(Distance d2, Distance d3) { inches = d2.inches + d3.inches; //add the inches feet = 0; //(for possible carry) if( inches >= 12.0) //if total exceeds 12.0, then decrease inches { inches = inches - 12.0; //by 12.0 and feet++; //increase feet by 1 } feet += d2.feet + d3.feet; //add the feet } /////////////////////////////////////////////////////////////////////// 21 int main() { Distance dist1, dist3; Distance dist2(11, 6.25); //define two lengths //define and initialize dist2 dist1.getdist(); //get dist1 from user dist3.add_dist(dist1, dist2); //dist3 = dist1 + dist2 //display all lengths cout << “\ndist1 = “; dist1.showdist(); cout << “\ndist2 = “; dist2.showdist(); cout << “\ndist3 = “; dist3.showdist(); cout << endl; return 0; } RUN: Enter feet: 17 Enter inches: 5.75 dist1 = 17’-5.75” dist2 = 11’-6.25” dist3 = 29’-0” 22 Member Functions Defined Outside the Class void Distance :: add_dist ( Distance d2 , Distance d3 ) { ---------23 } Objects as Arguments 24 Structures and Classes Structure Class The members are public by default as a way to group both data and functions the members are private by default struct foo { void func(); private: int data1; }; class foo { int data1; public: void func(); }; as a way to group data . 25 Classes, Objects, and Memory 26 Static Class Data 1. Each object contains its own separate data ( variables). 2. If a data item in a class is declared as static, only one such item is created for the entire class, no matter how many objects there are. 3. static data item is useful when all objects of the same class must share a common item of information 27 Static versus automatic member variables 28 class foo { private: static int count; //only one data item for all objects //note: “declaration” only! public: foo() //increments count when object created { count++; } int getcount() //returns count { return count; } }; /////////////////////////////////////////////////////////////////////////////////////////////////////// int foo::count = 0; //*definition* of count int main() { foo f1, f2, f3; //create three objects cout << “count is “ << f1.getcount() << endl; //each object cout << “count is “ << f2.getcount() << endl; //sees the cout << “count is “ << f3.getcount() << endl; //same value return 0;} 29 Here’s the output: 30 31