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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Structures  2 nd aggregate data type: struct  Recall:
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Chapter 8 Friends and Overloaded Operators. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Friend Function (8.1) Overloading.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Learners Support Publications Classes and Objects.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Friend Functions.  An ordinary function that is given special access to the private members of a class  NOT a member function of the class  Prototype.
Slide 1 Chapter 6 Structures and Classes. Slide 2 Learning Objectives  Structures  Structure types  Structures as function arguments  Initializing.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Chapter 7 Constructors and Other Tools Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
100 學年度碩士班新生暑期課程 程式設計 Object-Oriented Programming: Part I The Basics of Classes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
CSIS 123A Lecture 1 Intro To Classes Glenn Stevenson CSIS 113A MSJC.
Lecture Overview Linked List quiz Finish last class' topic:
COMP 53 – Week Two Operator Overloading.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Structures and Classes
User-Written Functions
Chapter 7: User-Defined Functions II
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
COMP 51 Week Twelve Classes.
CMSC202 Computer Science II for Majors Lecture 08 – Overloaded Constructors Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Review: Two Programming Paradigms
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
The dirty secrets of objects
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Introduction to Classes
Chapter 9 Objects and Classes
Learning Objectives Classes Constructors Principles of OOP
Constructors and Other Tools
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Friends, Overloaded Operators, and Arrays in Classes
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Submitted By : Veenu Saini Lecturer (IT)
CMSC 202 Lesson 8 Classes II.
Chapter 9 Introduction To Classes
CMPE212 – Reminders Assignment 2 due next Friday.
More C++ Classes Systems Programming.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
(4 – 2) Introduction to Classes in C++
Presentation transcript:

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 me immediately. ● Which leads to...

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; 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.

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.

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

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.

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.

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.

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).

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; }

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.

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.

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()

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.

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.

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.

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?

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.

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!

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.

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.

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(...);

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.

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.

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.

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!

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; }

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!

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.

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

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.

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);

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.

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.

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.

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;}... }

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.

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.

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.

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; }

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();

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.