CMP507 Data Structure and Algorithm in C Note #2 Dr. Peter Wang Introduction to Visual C++: C++ is a superset of C, everything available in C is also available in C++. Language design is an evolutionary process. A new language is often created by looking at the lessons learned from the past languages, or by trying to add newly conceived features to a language. Languages also evolve to solve specific problems. Hello.cpp #include void main() { cout << "Hello, World\n"; } Visual C++ Standard Edition Version Version 4.0 uses "project workspace" to manage the files that get compiled into an executable program. Integer.cpp // Dr. Peter Wang Feb. 28, 1997 #include void main() { int amount ; cout << "Enter an integer number: "; // prompt cin >> amount; // get data cout << dec << amount << ' ' // display << oct << amount << ' ' << hex << amount << endl; } In standard C, you can declare a function before you define it. The declaration describes the function's name, its return value, and the number and types of its parameters. This feature, called a "function prototype", allows the compiler to compare the function calls to the prototype and to enforce type checking. In C, function prototype is optional. C++ requires every function to have a prototype. Prototype.cpp #include void display(char *s); // function prototype required void main() { display("Hi, there !\n"); } void display(char * s) { cout << s; } Some simple enhancements and improvements that C++ offers the C programmer. o Default function arguments o More flexible placement of variable declarations o The scope resolution operator o Inline function o The const keyword o Enumerations o Function overloading Default function arguments:default.cpp #include void show(int month=9; int day=10; int year=53); void main() { show(); show(3); show(3,9); show(3,9,61); } void show(int month, int day, int year) { cout << " Month= " << month; cout << " Day= " << day; cout << " Year= " << year << endl; } Placement of variable declaration:localvar.cpp #include void main() { cout << "Enter a number : "; int n; cin >> n: cout << "The number is " << n << endl; } Scope resolution operator:scope.cpp #include int amount = 123; void main() { int amount = 456; cout << ::amount << endl; cout << amount << endl; } Inline function: inline.cpp #include #define MAX(A,B) ( (A) > (B) ? (A) : (B) ) inline int max (int a, int b) { if(a>b) return a; return b; } void main() { int i,x,y; x=23; y=45; i = MAX(x++, y++); // side effect cout << "x=" << x << "y=" << y << endl; x=23; y=45; i = max(x++, y++); cout << "x=" << x << "y=" << y << endl; } const.cpp #include void main() { const int SIZE = 5; char cs[SIZE]; cout << "The size of cs is " << sizeof cs << endl; } enum.cpp enum color { red, orange, yellow, green, blue, violet } #include void main() { color myfavorite; int i; myfavorite = blue; myfavorite = (color) 4; } overload.cpp #include #include char stringa[20], stringb[20]; void string_copy(char *dest, char *src) { strcpy(dest,src); } void string_copy(char *dest, char *src, int len) { strncpy(dest, src, len); } void main() { string_copy(stringa, "that"); stringb_copy(stringb,"this is a string",4); cout < stringa << "and" << stringb; } CLASS: The most important feature of C++ is its support for user-defined types, through a mechanism called " classes". Classes are far more powerful than the user-defined type you can create in C. An instance of a class is called an "object". Basically, a class allows you to create a structure and then permanently bind all related functions to that structure. This process is called encapsulation. construction.cpp #include #include class demo { char name[20]; public: demo(char *nm); ~demo(void); } demo::demo(char *nm) { strncpy(name,nm,20); cout << "construction called for " << name << endl; } demo:: ~demo(void) { cout << "Desstruction called for " << name << endl; } void func() { demo localfuncobject("localobject"); static demo staticobject("staticobject); cout << " inside func" << endl; } demo globalobject("globalobject"); void main() { demo localmainobject("localmainobject"); cout << "In main, before calling func" << endl; func(); cout << "In main, after calling func" << endl; } For local objects, the constructor is called when the object is declared and the destructor is called when the program exits the block in which the object is declared. The member visibility: The private and public labels in the class definition specify the visibility of the members that follow the labels. Private members can be assessed only by member functions and friend classes and functions . Public members can be assessed by member functions , and by any other functions in the program as long as an instance of the class is in scope. The public members determine how the class appears to the rest of the program. They make up the classes's interface. The function's prototype appears inside the declaration of the class, and when the function is defined, it is called by the c lass_name::function_name(). This indicates that it is a member of the class and that its name has "class scope" The class name combined with the scope resolution operator prevents any confusion between the definitions. You can also overload a member function, as long as each version is distinguished by its parameter list. The constructor: A constructor is a special initialization function that is called automatically whenever an instance of your class is declared. The constructor function prevents errors resulting from the use of uninitialized objects. The constructor must have the same name as the class itself. Whenever an instance of a class comes into scope, the constructor is executed. Date birthday ( 9, 10, 53 ); You can not specify a return type when declaring a constructor. You can overload the constructors. The destructor: Constructor is a member function that is called automatically when a class object goes out of scope. Its purpose is to perform any cleanup work necessary before an object is destroyed. The destructor name is the class name with a tilde (~) as a prefix. There is only one destructor for a class. A destructor takes no parameters and has no return value. By using member functions to control access to private data, you hide the representation of your class. Access function let you change the implementation of a class without affecting any of the programs that use it. This convention is known as "encapsulation", which is one of the most important principles of the object-oriented programming. Inherritance: The object-oriented language try to make existing code easily modifiable without actually changing the code. This is a unique and very powerful concept, because it does not seems possible to change something without changing it. Using two new concept however - inheritance and polymorphism - it is possible to do just that. New classes can modify the behavior of their base classes, a capability known as polymorphism. From a design perspective, private derivation is equivalent to containment except for the issue of overriding. An important use of this is the techinique of deriving a class publicly from an abstract base class defining an interface and privately from a concrete class providing an implementation. Because the inheritance implied in private derivation is an implementation detail that is not reflected in the type of derived class, it is sometimes called "implementation inheritance" and contrasted to public declaration, where the interface of the base class is inherited and the implicit conversion to the base class is allowed. The latter is sometimes referred to as subtyping or "interface inheritance". Operator overloading: This feature lets you specify new ways of using standard operators in your programs. In this way, new types are added to the language in a completely seamless manner. The overloading concept extends to all functions created in C++. Function overloading: One of the most powerful new features in C++ is called "function overloading." An overloading function has several different parameter lists. The language distinguishes which function to call based on patter-matching the parameter list types. Virtual functions: The word virtual in front of a function tells C++ that you plan to create new versions of this function in derived classes. That is, it lets you state future intentions for a class. When the cirtual function is called, C++ looks at the class that called the function and picks the version of the function for that class, even if the derived class did not exist at the time the function call was written.