CS302 Topic: More on C+ + or, “Your life as a CS302 student…” Tuesday, Sept. 13, 2005
Announcements � Lab 2 (Golf Handicaps) is due this Friday, Sept. 16!! � Lab 3 (Stock Charts) will be made available soon � It will be due next Friday, Sept. 23!! � Don’t get behind!!
More C+ + � Naming conventions (suggestions) � More about classes � Privileges (incl. “friend”) � “this” � inlining � string class � More class examples � Templates
Naming Suggestions (commonly used, but not required) � Class, variables, functions � Concatenate all words without using underscore (“_”) � Don’t capitalize first word � Capitalize all other words � Examples: � dList � rbTree � screenCursor � assignNum() � Constants: � Capitalize all letters, use underscores between words � Examples: � const int HIGH_SCORE = 100; � const int BOILING_POINT = 212;
More about classes � Class object can be declared as a data member only if its class definition has already been seen � Example (BAD CODE!): class Stack { int topStack; Stack stack; // illegal --class definition not yet complete }; � � Pointer Pointer to a class object can be declared as a data member as long as a forward definition of the class has been seen: � Example (good code): class Stack; // forward declaration class Stack { int topStack; Stack *stack; // legal : forward class def’n. has been seen }; � Example (good code): class tree_node { tree_node *left_child; // legal : class declared when name seen tree_node *right_child; };
Class privileges � Notes: “Class member” refers to data elements or methods within a class � Types of access privileges for a class member: � public: � Class member accessible from anywhere within the application � protected: � Class member accessible from any of the class’s methods; � Class member may be inherited by subclass � Not available elsewhere in application � private [ default] : � Class member only available to class’s methods � Not inherited
Using “friend” to grant access � “friend”: � Gives an outside class access to a class’s protected and private members � Is NOT reciprocal � Example: class Dlist; class Dlnode { friend class Dlist; protected: ... }; � Here, Dlist gains access to all of Dlnode’s variables and methods � Typically used when outside class “owns” the current class
Can also define “friend functions” � Allow nonmember functions access to private/ protected members of class � Example: class myClass { int a, b; //default to private public: myClass(int I, int j) {a=I; b=j;} friend int comDenom(myClass x); }; int comDenom(myClass x) { //Note that since comDenom is a friend function //of myClass, it can directly access a and b int max = x.a < x.b ? x.a : x.b; for (int i=2; i <= max; i++) if ((x.a%i) == 0 && (x.b%i) == 0) return i; return 0; }
Implicit “this” pointer � “this”: a pointer to the object through which this method was invoked � Example: the following are equivalent: int getSize() { return size; } int getSize() { return this->size; } � Passed as an implicit argument to every member method � Common uses for “this”: � To call a method in another object, passing the current object as an argument � E.g.: objectTable.add(this) � To return a pointer to the current object � Note: friend functions don’t have a “this” pointer, because friends are not members of a class. Only member functions have a “this” pointer
Inlining � Inline function: expanded inline at the point at which it is invoked, rather than being called � Advantage: efficiency (eliminates function call) � Frequently used in class definitions � Two ways to create inline functions: � Use “inline” modifier: inline int f() { … } � Define inline function within a class (any function defined inside class definition is automatically made into an inline function) class cl { int i; Inline function definitions public: int get_i() {return i;} void put_i(int j) { i = j; } };
A bit on C+ + Memory Management � We’ve seen new and delete � C+ + does some memory management for you � E.g., when you declare a local class object (e.g., inside a procedure), C+ + will automatically call the destructor for that class when the procedure exits � C+ + string class handles memory management for you � E.g., to ensure that enough memory is made available for your string, regardless of how long it is (or gets)
C+ + string class � 1 st -class vs. 2 nd -class objects: � 1 st -class objects: � Can be manipulated in “usual” ways � E.g., � assignment operator (= ) � copy constructor to make complete copies � destructor that performs memory management � comparisons (< , = = , > , != , … ) 2 nd class objects: � � Can be manipulated only in certain restricted ways
C+ + string class � Two types of strings: � 2 nd -class objects: C-style, null-terminated string � #include <string.h> // C version � Common functions: strcpy(), strcat(), strcmp(), strlen() � 1 st -class objects: “string” class � A string is like a (char * ) that has been made into a safe C+ + class � Part of C+ + class library � Provides object-oriented approach to string handling � #include <string> using namespace std; // C++ String class � Common functions: length, c_str, =, +=, ==, !=, <, <=, >, >=, [ ]
Example 1 (hw2): Using string class #include <stdio.h> #include <iostream> #include <string> using namespace std; Can assign string to a standard null-terminated C string main() { string str1, str2; Use length() to get string length const char *s2; char *s3; To printf, have to convert str1 = "Hello World"; to (char *); c_str() does printf("%d %s\n", str1.length(), str1.c_str()); cout << str1.length() << " " << str1 << "\n"; this for you str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; Can print with cout without s2 = str2.c_str(); converting to (char *) cout << s2 << " " << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; Can assign one str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; string to another str2 += " "; str2 += str1; cout << str2 << "\n"; }
Example 1 (hw2): Using string class #include <stdio.h> #include <iostream> Can modify individual #include <string> using namespace std; characters of string by treating them as (char *) main() { string str1, str2; const char *s2; char *s3; Can turn string to (char *) and assign to a (char *) str1 = "Hello World"; printf("%d %s\n", str1.length(), str1.c_str()); variable; however, can only cout << str1.length() << " " << str1 << "\n"; do this with const variables str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s2 = str2.c_str(); When you assign a string cout << s2 << " " << str2 << "\n"; from a (char *), it makes a s3 = "Daffy Duck"; str2 = s3; copy str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; Can use + operator to str2 += str1; concatenate strings cout << str2 << "\n"; }
Example 1 (hw2): Using string class #include <stdio.h> Output? #include <iostream> 11 Hello World #include <string> 11 Hello World using namespace std; Hello World Jello World main() Hello World { string str1, str2; Hello World Hello World const char *s2; Daffy Duck Taffy Duck char *s3; Taffy Duck Jello World str1 = "Hello World"; printf("%d %s\n", str1.length(), str1.c_str()); cout << str1.length() << " " << str1 << "\n"; str2 = str1; cout << str2 << "\n"; str1[0] = 'J'; cout << str1 << "\n" << str2 << "\n"; s2 = str2.c_str(); cout << s2 << " " << str2 << "\n"; s3 = "Daffy Duck"; str2 = s3; str2[0] = 'T'; cout << s3 << " " << str2 << "\n"; str2 += " "; str2 += str1; cout << str2 << "\n"; }
Recommend
More recommend