C Jumpstart C uses class and object declarations

C++ Jumpstart • C++ uses class and object declarations similar to those of Java, but the object hierarchy of C++ is not singly rooted. That is there is no unique Object class. • Terminology: – members (instance variables) • data members and member functions – derived class (subclass) – base class (superclass) Lecture 6 PLP Spring 2004, UF CISE 1

Member Access Protection class A { public: // anyone can manipulate these int pub_a; protected: // derived classes can manipulate int prot_a; private: // only this class can manipulate int priv_a; }; • Access protection controls access to both data members and member functions. Lecture 6 PLP Spring 2004, UF CISE 2

Object Constructors • As in Java, the constructors for a class have the same name as the class itself. • Unlike Java, since there is no garbage collector, a destructor function can be written to deallocate data that is associated with an object. The name of the destructor is the class name prepended with a tilde (~). Lecture 6 PLP Spring 2004, UF CISE 3

Separation of Declaration and Definition • Unlike Java, in a C++ program it is expected that the declaration of a class will be distinct from the definition of most of its member functions. This distinction between declaration and definition is a critical one. • A C++ declaration provides all information necessary to use an object in a given way. For a class, there can be several different levels of declaration: – Class name only (allows pointers to objects of the class to be used) – Class name and structure including all data and function members (allows space for variables containing objects to be allocated and code calling member functions to be generated. • The definition of a class (which may appear in several places) provides function definitions for each member function. Lecture 6 PLP Spring 2004, UF CISE 4

Sample Class Declaration This would go in file Sample. h #include <iostream> using namespace std; class Sample{ private: int a; int b; char *s; public: Sample(int aval, int bval, char *sval); void print(ostream &o) const; }; • This declaration does not define the member functions. • The word const following the print member function declaration tells us that the print function will not be able to change the value of the object upon which it is dispatched. • If the bodies of functions Sample and print are given in the declaration, they are compiled inline in all uses. Lecture 6 PLP Spring 2004, UF CISE 5

Rest of the Class Definition This would go in file Sample. c #include "Sample. h" Sample: : Sample(int aval, int bval, char *sval): a(aval), b(bval), s(sval) {} void Sample: : print(ostream &o) const { o << a << "t" << b << "t" << s << endl; } • The : : operator is used to resolve the scope of an object. In the cases above, it’s used to identify the class that contains the member functions being defined. • The list following the : in the constructor declaration specifies initial values for the object data members. • When a parameter is declared to be of type t & , that means that the argument of type t is passed by reference. Lecture 6 PLP Spring 2004, UF CISE 6

Declaring, Allocating, and Manipulating Objects #include "Sample. h" int main() { Sample sobj(1, 2, "abc"); Sample *sptr; sobj. print(cout); sptr = new Sample(3, 4, "def"); sptr->print(cout); } • All variables represent value containers. • Storage must be explicitly allocated for pointers before they can be dereferenced. Lecture 6 PLP Spring 2004, UF CISE 7

Standard Template Library • • The C++ Standard Template Library (STL) contains numerous classes that provide capabilities necessary for a variety of programming tasks. Many of the STL classes are container classes (they hold data structure objects). You will be well-served to exploit the STL in developing your programs. The STL uses C++ template classes. A template class is a class that is well -defined up to the value of a parameter. Class parameters may be types or value. Think of the typical data structuring concept of a Stack of Integers. A template class for representing Stacks would take the type (Integer) as an argument to its parameter. In C++, one might declare such a class as • class Stack<class T>. . . }; Objects belonging to the class are declared like this: • • • Stack<int> s; Lecture 6 PLP Spring 2004, UF CISE 8

STL map class • The STL map class can be used to represent discrete mappings from one type of object to another type. The map contains elements. Each element has a key (the mapped class) and a value (the value to which the key is mapped). • The following example shows how one can use a map of strings. Lecture 6 PLP Spring 2004, UF CISE 9

#include <iostream> #include <stdlib. h> using namespace std; #include <map. h> #include <string. h> int main() { map<const string, int> keywords; keywords["break"] = 1; keywords["class"] = 2; cout << "value of class is" << keywords[class] << endl; cout << "value of foobar is" << keywords[foobar] << endl; } • Execution of this program yields the following output: value of class is 2 value of foobar is 0 Lecture 6 PLP Spring 2004, UF CISE 10

Flex and Good Programming • Wilson’s Law: Don’t mess with workin’ code. • One problem in the sample lexical analyzer provided earlier is that changing the language (by adding or deleting keywords) causes one to need to change the rules of the language. if|then|begin|end|procedure|function { cout << "A keyword: "<< yytext << endl; • This would be better handled by a single simple rule that handles both idenfitiers and keywords: [A-Za-z][A-Za-z 0 -9]* { if (keyword[yytext]) { cout << A keyword: << yytext << endl; } else { cout << An identifier: << yytext << endl; } } Lecture 6 PLP Spring 2004, UF CISE 11

Concision in Your Lexical Analyzer • Wilson’s Law: Any line of code you don’t write can’t have a mistake in it. • Don’t add lots of extraneous rules in your lexical analyzer. Write as few rules for any type of token as you can. • You should have just one rule for floating point numbers, • You might have four rules for integers (decimal, hexadecimal, octal, and binary) • Your main program should do as little as possible. Lecture 6 PLP Spring 2004, UF CISE 12

Words to the Wise • Seen (by me) on a CDC 713 tty at the Florida State University in the Fall of 1974: Don’t work, shirk. Don’t create imitate. Follow the path of least resistance. • Up to, but not including, cheating or thievery, this is good advice. If you see some code that I’ve given in examples that you can use in your lexical analyzer, use it! Lecture 6 PLP Spring 2004, UF CISE 13
- Slides: 13