Presentation is loading. Please wait.

Presentation is loading. Please wait.

General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing.

Similar presentations


Presentation on theme: "General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing."— Presentation transcript:

1 General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing that you think should be there, or any sort of error, please email me immediately. ● Which leads to...

2 General Updates ● Extra Credit – In the event of typos/incorrect information in assignments, quizzes, posted slides, or the like, please let me know. Finding an error so I can fix it is worth 1-5 lab points depending on the severity of the error. – To get you in the habit of programming, I will let you choose to do your own extra credit programs. I will make these worth 1-20 lab points; email me or see me in my office hours and we can arrange a problem – for inspiration on a program to do, see the Programming Projects at the end of chapters in your textbooks.

3 Structure Rehash ● Last class we talked about structures, and how they're essentially data types that contain multiple member variables. ● All members of the structure were publically available via the dot operator. ● Essentially a way to associate data with other data.

4 Classes ● This is the data type that truly differentiates C++ from C ● Classes are central to object-oriented programming.

5 What is a class? ● A class is similar to a structure in that it has a collection of member variables. ● A class is different from structures in that it also has member functions. ● The value of a class variable (and sometimes the class variable itself) is known as an object. When programming with classes, your program is viewed as a collection of interacting classes – hence, object-oriented programming.

6 Defining a class ● Here is a basic class definition: class DayOfYear{ public: void output(); int month; int day; }; ● This class has two member variables and one member function.

7 Member Functions ● These are functions that the object can perform – the object is, in a way, now capable of taking actions. ● Member functions will called much like member variables of a struct were used – using the dot operator.

8 Using DayOfYear ● So we've declared a DayOfYear class. How do we use it? – First, we define the member functions. – Second, we declare it in a program. – Third, we can invoke it's member functions (or possibly alter its member variables).

9 Defining Member Functions ● Somewhere in the code following the class definition, you will define the actions of the member function. void DayOfYear::output(){ if(month 12 || day 31){ cout << “Error in output()!”; else cout << month << “/” << day << endl; }

10 Member Function definition ● First, you'll notice a new operator, the scope resolution operator, or :: symbol. ● This operator is used to define what scope (often what class) the portion following the symbol is associated with. ● In this case, we are telling the compiler that this code for output() is associated with the DayOfYear class. ● The scope resolution operator is often called a type qualifier, as it specializes (qualifies) your function to a certain type.

11 Member function definition ● Notice that we did not have to specify the object or use the dot operator to access the member variables in the function. ● This is because when you call the function, a specific object will do the calling. All member names in the function definition will thus be taken from that object, as though you were not calling month but object.month.

12 How To Use The Class ● Now that we have defined the member functions in DayOfYear, we can use it in a program: int main(){ DayOfYear today; today.month = 6; today.day = 21; today.output(); return 0; } ● This program would: – Create an object “today.” – Set the object today's variables – Have today perform the action output()

13 Using your class ● As said before, using a class works like using a struct. ● Use the dot operator to access (public) variables, and to call (public) member functions.

14 Classes are Types ● Just like structs before, a class is a type, like int or double are. ● This means your you can have variables of a class type, pass the class type as a parameter, return it from a function, and so on. ● You have effectively defined a brand new type, one that has actions associated with it.

15 Why Is This Different? ● The main difference (so far) is the notion that you are no longer performing an output algorithm by looking at the variable and using the data, but rather that the data is taking an action itself.

16 Encapsulation ● Classes are also implemented in such a way as to improve data abstraction, which we earlier defined as when you write code such that a programmer who uses your functions doesn't need to know how it works. ● In classes, we can now take it a step further, and make it so that a programmer does not need to know how the object represents its data, or how the functions work. ● It is thus usually best to make your member variables hidden in some way. How?

17 Public/Private ● Recall that we used the code public: when we defined our class earlier. ● C++ provides a way by which we can specify what parts of code in a class are accessible outside of the class. – Public: These can be accessed anywhere. – Private: These can be accessed only from functions within that class' scope. – There is a third called protected that we will work with later on.

18 How is private used? ● Let's change DayOfYear so that only output can be used: class DayOfYear{ public: void output(); private: int month; int day; }; ● Now month and day cannot directly be changed. ● Of course, now we have no way to set the month and day!

19 Accessor and Mutator Functions ● These are functions in a class which will allow access (if needed) to retrieve and change the values of private member variables in a class. class DayOfYear{ public: void setMonth(int newMonth); void setDay(int newDay); int getMonth(); int getDay(); void output(); private: int month; int day; }; ● Why bother with this? Two possible reasons: – Error checking: you can insure correct input! – Return types: store as an int, return as a string.

20 Encapsulation again ● Think of class design as designing interfaces and implementation: – Interface is how you want the class to be accessed and used – the comments about the class, and the public member function and their comments. – Implementation is how you store data for and code the functions of the class. ● The benefit is easier code to modify and use – If your code is properly encapsulated, you should be able to change the implementation without any code that uses the class noticing a difference.

21 Class Syntax ● How to use a class, then: class { public: Public_Member_Specification1; Public_Member_Specification2;... private: Private_Member_Specification1; Private_Member_Specification2;... }; ● Member specifications are variables or prototypes. ● When defining the functions, use :: Return_Type Class_Name::Function_Name(...);

22 Simple Class Use ● Call functions (or edit any public variables) using the dot operator. – To call functions just use type_var.func_name(arguments); ● Classes can be used like any other type, including dynamic allocation with new and pointers, in which case the arrow operator gives the same access as the dot operator.

23 Constructors ● So far, we have only modified/initialized a class' variables by manually setting the member variables or by using mutator functions. ● C++ provides us with a special function called a constructor that is used at variable declaration time to perform initialization.

24 The two rules of constructors ● 1) The function name must be the same as the class. ● 2) The constructor definition cannot return a value. There should be no return type given. Class DayOfYear{ public: DayOfYear(int monthValue, int dayValue); void output(); void set(int newMonth, int newDay); int getDay(); int getMonth(); private: int month; int day; } ● Note that typically, constructors are public.

25 Initializing with a constructor ● Initializing your data is much easier now: DayOfYear today(6,21), yesterday(6,20); This sets your object for you. ● Note: you cannot just call a constructor like any other function! today.DayOfYear(6,21) // ILLEGAL!

26 Defining the Constructor ● You define the constructor as you would any other member function, following the two rules about constructor definitions: DayOfYear::DayOfYear(int monthValue, int DayValue){ month = monthValue; day = dayValue; }

27 A shorter way to do initialization ● If you're just passing in values to set your object's member variables to, there's a quick way to do it. DayOfYear::DayOfYear(int monthValue, int dayValue) :month(monthValue), day(dayValue) {} ● The second line here is called the initialization section. You can use it by placing the colon, followed by member variables and what they should be set to in parentheses. You can use the parameters!

28 Constructors ● You'll notice in the last example that the body of the constructor was empty. That's because all the setting was done in the initialization section. ● However, you shouldn't necessarily leave it blank. – You may still need to do calculations for your object. ● Perhaps you have extra parameters that aren't just what the member variables to be set to. – You should do error checking! ● Make sure all parameters are legal for what the class should represent.

29 Constructors can be overloaded! ● It's possible to overload a constructor just like a regular function. ● This allows you to define multiple ways to initialize an object – useful if there might be many ways for a user to easily represent the data. ● If you overload the constructor (and you typically will), you should also make sure to create a default constructor – a constructor that takes no parameters. – If you don't, and you try to initialize an object without passing parameters, there will be an error. DayOfYear date; //Only legal if a default exists

30 About default constructors ● If your object has no constructors, one is created automatically that simply allocates the variable and leaves it uninitialized. ● If you have other constructors, as said, you must create this constructor manually.

31 Using the Constructors ● Calling a default constructor: DayOfYear date; ● Calling a specific constructor: DayOfYear date(userDay, userMonth); DayOfYear date(month); ● Explicit invocation date = DayOfYear(); // Invoking default date = DayOfYear(userDay, userMonth);

32 Classes as member variables ● You can have a class as a member variable of another class! ● Typically you don't need to do anything special other than include it and make sure you set/initialize it nicely in your constructor. ● If you want to use it in an initialization section, you can call that class' constructor.

33 Initializing a class with a member class ● class Holiday{ public: Holiday(); // Default constructor Holiday(int month, int day, bool presents); void output(); private: DayOfYear date; bool presentsGiven; } ● How can you initialize date in the init section? Holiday::Holiday(int month, int day, bool presents) :date(month, day), presentsGiven(presents) {...//Check for allowable parameters...} ● You can also do date = DayOfYear(month, day); in the definition.

34 Constant member functions ● There is a technique very similar to constant parameters for class member functions. ● Add the modifier const after a function declaration/definition for the member function – It prevents code that would change the object invoking the function. void output() const; void input(); void setMonth(int newMonth); int getMonth() const; ● Like the const parameter modifier (Ch. 4), you should use it consistently or not at all.

35 Inline functions ● We won't be using inline functions in this class, but you should know what they are and how they work. ● Inline functions are when you directly define a function in the class when you might normally give its prototype. – Note that a inline function need not be const, it just is in this example. class DayOfYear{ public: int getMonth() const { return month;}... }

36 Inline Functions ● The compiler treats inline functions specially: – Whenever the function invocation occurs, it replaces the invocation with that code directly. – In theory, this reduces the overhead associated with a function call and is better/more efficient. – In reality, this goes against encapsulation, and also works differently across compilers. ● Some compilers let you use inline functions anywhere. ● Some only let you use them within the same file.

37 Static Member Variables/Functions ● Static variables are variables shared by all members of a class. They can be private, to gain the advantages of a global variable, but one that is private only to objects of that class. ● Static functions are functions that do not use any of the data specific to an object in the class it is defined in.

38 Using static member variables ● To declare them: class Server{... private: static int turn;... } int Server::turn = 0; ● They can be public, but then you essentially just have a global variable. ● All class members will now share an integer called turn. ● Note that you must define the variable outside the class definition.

39 Using static member functions ● To declare them: class Server{ public: static void getTurn();... private: static int turn;... } int Server::turn = 0; void Server::getTurn(){ turn++; return turn; }

40 Using Static Member Functions ● Note that static functions can only use static variables in their code. ● getTurn can now be invoked outside the class. While this is not unusual (it is public), since it is static you need not (and usually should not) invoke it with a particular object. ● Instead of int currTurn = object.getTurn(); call int currTurn = Server::getTurn();

41 Nested/Local Class definitions ● It is possible to declare an entirely new class within a class definition, known as a nested class. ● You can also declare a new class within a function, known as a local class. ● Local classes are confined to use within the function, and may not contain static members. ● Nested classes are accessible outside the class if they are public. ● We may see nested classes much later in the semester.


Download ppt "General Updates ● The Wiki is now back up to date. I have also added a working link to the lecture slides online. ● Whenever you notice something missing."

Similar presentations


Ads by Google