Presentation is loading. Please wait.

Presentation is loading. Please wait.

C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures.

Similar presentations


Presentation on theme: "C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures."— Presentation transcript:

1 C ++ Basics by Bindra Shrestha shrestha@uhcl.edu http:// sce.uhcl.edu/shresthab shrestha@uhcl.edu CSCI 3333 Data Structures

2 Acknowledgement Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Ms. Krishani Abeysekera

3 Assumptions Assume that you are familiar with C, C++ or Java.

4 Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s “C with classes”, Better C Standardized by ISO in 1997  Includes the C++ standard library  Standard Template Library (STL) Part of the C++ standard library Ready made classes for data structures and algorithms

5 C++ Filenames Can be different from the name of the class or the name of the function in the file.cpp  Extension for the C++ source code files.h  Extension for C++ header files  Usually, code for a data structure is put in a header file, and then it can be added to a program with an include directive, e.g. #include "BasicVector.h" Name of executable file  In MSVS it’s the name of your project  In g++ it’s either “ a.out ” or a name you specify

6 A Simple C++ Example /* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional }

7 /* FILE: main.cpp */ #include using namespace std; int main() { cout << "Enter your first name: "; string name; cin >> name; cout << "Hello, " << name << endl; return 0; //optional } A Simple C++ Example Comment C++ header files from C++ standard library “Entry” function Namespace for C++ standard library All C++ statements end with a semicolon

8 Examples…Continued //Variables can be declared anywhere using namespace std; #include int main () { double a; cin >> a; a = (a + 1) / 2; double c; c = a * 5 + 1; cout << "c contains : " << c << endl; int i, j; i = 0; j = i + 1; cout << "j contains : " << j << endl; return 0; }

9 Example…Variable Scopes using namespace std; #include int main () { int i = 5; for (int i = 0; i < 5; i++) // Local { cout << i << endl; // 0, 1, 2, 3,4 } cout << i << endl; // This outputs 5 return 0; }

10 Example…Control Structures //for, if, do, while, switch, and Exception using namespace std; #include #include int main () { int a; cout > a; try { if (a >= 5) throw 5; if (a < 5) throw a; throw a * 2; } catch (int e) { cout << “Exception caught: " << e << endl; } return 0; }

11 Example…String Supports C-style, array of characters string Also C++ provides STL Strings, a part of Standard Template Library #include using std::string; string s = “to be”; string t = “not” + s; string u = s + “ or “ + t; if (s > t) cout << u; …Also, s = “john” Int i = s.size( ); // i = 4 Char c = s[ 3 ]; // c = ‘n’ S += “ smith” Char *p =s.c_str( ); //string

12 Example – Function Overloading Two or more functions are defined with the same name but different argument lists. using namespace std; #include double addOrSubtract (double a, double b) { return a + b; } int addOrSubtract (int a, int b) { return a - b; } int main () { double x = 5, y = 4; int k = 5, p = 3; cout << “Added:” << addOrSubtract(x,y) << endl; cout << “Subtracted:” << addOrSubtract(x,y); return 0; }

13 Example - Operator Overloading Allows overloading of operators such as +, *, +=, and <<. E.g. comparison – “p1 == p2” where p1 and p2 are objects. bool operator ==(const Passenger &x, const Passenger &y) { Returnx.name == y.name && x.mealPref = y.mealPref; }

14 Example - Operator Overloading Another useful application is for defining input and output operators of classes and structures. The type iostream is output stream type, for example cout is of this type. ostream& operator <<(ostream &out, Passenger &pass) { Out << pass.name << “ “ pass.mealPref; Return out; } Example: cout << pass1 << pass2 <<‘\n’;

15 Example…Structure using namespace std; #include struct sample { int a; int b; int multiply () //FUNCTIONS OR METHODS { int c; c = a * b; return c; } }; int main () { sample sam1; sam1.a = 2; sam1.b = 4; cout << “Result: " << sam1.multiply() << endl; return 0; }

16 Example…Class using namespace std; #include class sample { public: int a; int b; int multiply () //FUNCTIONS OR METHODS { int c; c = a * b; return c; } }; int main () { sample sam1; sam1.a = 2; //public, so possible sam1.b = 4; cout << “Result: " << sam1.multiply() << endl; return 0; }

17 Functions Argument Passing  By default, arguments in C++ are passed by value.  When passing the argument by reference, modifications to the arguments in the function would modify the actual argument void f( int value, int &ref) { //one value, one ref value ++; ref ++; }

18 Constant Reference Arguments Structure and class arguments are usually passed by reference. Efficiency ? Passing by reference is much more efficient since only the address of the structure need to be passed. An even better practice? Pass by argument as a “constant reference”, preventing modification. void f ( const Passenger &const){ Pass.name = “new name” //not allowed } Furthermore, passing the argument to another function that might modify is NOT allowed.

19 Array Arguments Can arrays be passed by value? No. When an array is passed to a function, it is converted to the pointer to its initial element. T[ ] is converted to T*. Thus functions can modify original array.

20 Questions and Comments?


Download ppt "C ++ Basics by Bindra Shrestha sce.uhcl.edu/shresthab CSCI 3333 Data Structures."

Similar presentations


Ads by Google