Presentation is loading. Please wait.

Presentation is loading. Please wait.

What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.

Similar presentations


Presentation on theme: "What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed."— Presentation transcript:

1 What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed Calculations Made decisions

2 From now on, our programs will include not only a main function, but also one or more classes, each containing data members and member functions. Now we enter object- oriented programming

3 Our code, when fully developed will represent a grade book that a teacher can use to record and organize student test scores. Our main function will create the GradeBook object which is a class. The member function of the GradeBook class we will use today is called displayMessage Before the main function can create a GradeBook object, the member functions and data members that belong to the GradeBook class must be defined. Class definition is accomplished the keyword, class, followed by the class name, GradeBook. Data members are the objects kept track of inside the class. Every class’s body is enclosed in a pair of left and right French braces. {} The class definition ends with a semicolon ; NOTE: this is a common syntax error – forgetting the semicolon!

4 Naming conventions: User-defined class begins with a capital letter and each following word in the class name is capitalized -> GradeBook Member function names begin with a lowercase first letter, with following words capitalized -> displayMessage Access specifiers -> always followed by a colon : public /*member functions appearing after this specifier can be called by other functions */ private /* only accessible to member functions of the class in which they are declared, also called “data hiding”*/ protected Return types -> void //function does not return a value int // function returns an integer value

5 Defining a Member Function with a Parameter Let’s talk about cars…

6 A function call needs values, called arguments, for each of the parameters, the function will use! Each function header needs both a type and a name. New class, a variable type called string, is actually a class defined in #include This class is also part of namespace std! The initial value of a string is the empty string.

7 Why getline instead of cin?? cin reads characters until the first white- space character is reached. The function call from getline(cin, nameOfCourse) reads characters including spaces until the newline character is found. (The newline character is not stored in nameOfCourse.)

8 Function Parameter list: Can be no parameters at all, or several! Each parameter specifies a type and an identifier. For our example, type string and identifier courseName. The parameter variable’s name (courseName) can be the same or different from the argument variable’s name (nameOfCourse).

9 Variables declared in a function definition’s body are known as local variables, and can only be used in the function! When the function terminates, the local variables’ values are lost. A class normally has one or more member functions that manipulate the attributes of that class’s objects. These attributes are called data members and are declared inside a classes definition. The :: operator is called the binary scope resolution operator. A local variable is declared in the body of a function and can only be used inside that function. A data member is declared in a class, but not in the body of any of the class’s member functions. Every object of a class has a separate copy of the class’s data members. Data members are accessible to all member functions of the class. A parameter represents more information that the function needs to do its work! Each parameter that a function needs is stated in the function header. An argument is the value supplied to the function call. When the function is called, the argument value is passed into the function parameter so that the function can do its thing.


Download ppt "What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed."

Similar presentations


Ads by Google