Professional Documents
Culture Documents
Fundamentals of C++: Yingcai Xiao 09/03/08
Fundamentals of C++: Yingcai Xiao 09/03/08
Fundamentals of C++: Yingcai Xiao 09/03/08
Outline
Class Definition IO Template vector C Pointer Dynamic Memory Allocation
Example: How to define a class (user-defined data type) class Rectangle { protected: int width; int height; public: Rectangle () { } Rectangle (int cx, int cy) {width = cx; height = cy;} int area() {return with*height;} };
Rectangle ()
Rectangle (int w, int h) Area()
rect is the name of a memory space that stores a Rectangle object.
Console IO
Reading Input: cin
console input an object of iostreams only supports one operator >> reads input for its arguments, one at a time auto converts input to the type of the arguments Displaying Output: cout console output an object of iostreams only supports one operator << sends output to the console for its arguments, one at a time auto converts output of the type of the arguments for display display format can be specified
File IO
#include <string> #include <fstream> // file io streams #include <iostream> // file io streams using namespace std; int main() { string infile, outfile; cout << "Enter input file name: \n"; cin >> infile; cout << "Enter output file name: \n"; cin >> outfile; ifstream in(infile.data()); // Open for reading ofstream out(outfile.data()); // Open for writing string s; while(getline(in, s)) // Discards newline char out << s << "\n"; // ... must add it back } ///:~
C in C++
Data storage types: int, float, char, array (e.g., int ia[4];), enum, struct, union No classes in C. Computation operators: Math: +, -, *, /, %, =, -=, +=, *=, /= Logical: <, >, <=, >-, ==, !=, ||, &&, bitwise: |, &, >>, <<,^ Control statements: if-else, while, do-while, for, switch,
Example: int i = 8;
i (4 bytes of memory)
8
The memory of a variable is allocated at compile time. The memory of an automatic variable is automatically freed at run time when the variable is no longer in used. By default all variables are automatic. The following statements are the same: int i = 8; auto int i = 8;
ia[0] 1
Address of a Variables
&
Operator & returns the address of an variable e.g.: &i, &ia[0], &ia[1] The address of a memory is expressed as a hex number e.g.: 0xffaabbcc
Example: Dog *dp; dp = new Dog(Stommy); cout << (*dp).getName(); cout << dp->getName(); Exam Pet.cpp
class Pet { // define a class, group everything together, encapsulate as needed. protected: string name; // protected members can be accessed by child classes public: Pet() {name = I have no name.";} // default constructor Pet(string nm){name = nm;} // constructor string getName() {return name;} // to be inherited by child classes //virtual method, to be overridden by child classes, for polymorphism virtual string speak() const { return I can't speak.; } };
cout << We put all the pets together to make it easier to take care of them (code reuse). \n; vector<Pet> pets; // vector mimics variable-size array, its type is a variable too (template)
pets.push_back(d); pets.push_back(c); pets.push_back(p); for(int i=0; i<pets.size();i++) cout << "Pet " << i << " (" << pets[i].getName()<< ") " << " says " << pets[i].speak() << endl;
cout << "The above did not work correctly. No one can speak now. \n; cout << Incorrect use of virtual methods for polymorphism. \n";