1 Writing a Good Program 5. Objects and Classes in C++

Slides:



Advertisements
Similar presentations
Lesson 13 Introduction to Classes CS1 Lesson Introduction to Classes1.
Advertisements

Writing a Good Program 6. Pointers and Arrays
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Writing a Good Program 5. Objects and Classes in C++
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
C++ Classes in Depth. Topics Designing Your Own Classes Attributes and Behaviors Writing Classes in C++ Creating and Using Objects.
Object-oriented Programming Concepts
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Abstract Data Types and Encapsulation Concepts
C++ fundamentals.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures C++ Review Part-I.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Chapter 10 Introduction to Classes
CS1201: Programming Language 2 Classes and objects By: Nouf Aljaffan Edited by : Nouf Almunyif.
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.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
SNPL1 Woochang Lim C+OOP = C++ C (non OOP)  C++ (non OOP+OOP)  Java (OOP) Object-Oriented Design  Object-Oriented Programming Programming with C++
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Classes - Part II (revisited) n Constant objects and member functions n Definition Form of Member Functions n friend functions and friend classes n Constructors.
Lecture #6 Classes and Objects.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
C++ Features Function Overloading Default Functions arguments Thinking about objects – relationship to classes Types of member functions Constructor and.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Classes in C++ By: Mr. Jacobs. Objectives  Explore the implications of permitting programmers to define their own data types and then present C++ mechanism.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
C++ Classes & Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
Introduction to Classes
CS1201: Programming Language 2
Lecture 9 Concepts of Programming Languages
Introduction to Classes
Object-Oriented Programming
Introduction to Classes and Objects
Object-Oriented Programming
Object-Oriented Programming
CS1201: Programming Language 2
Chapter 9 Introduction To Classes
Classes and Objects Systems Programming.
Lecture 9 Concepts of Programming Languages
Presentation transcript:

1 Writing a Good Program 5. Objects and Classes in C++

2 5.1 Basic Object Oriented Programming Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

3 What is an Object? The real world is composed of different kinds of objects: buildings, men, women, dogs, cars, etc. Each object has its own states and behaviors. Color = Red Brand = Ferrari Speed = 200 mph Gear = 4 States Braking Accelerating Changing Gear Steering Behaviors Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Variables and functions

4 What is a Software Object? Software designers use the same idea to ease programmers to develop their software Software is also composed of different kind of software objects Each software object also has its own states and behaviors. Variables (States) Color = Grey Size = 2cm x 2cm Shape = Rectangular Method (Behavior) (protruded) Press( ) Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Button

5 Encapsulation Hiding information within an object’s nucleus Provide a public interface for interacting with it Advantages to software developers Modularity: An object can be easily passed around in the system (Otherwise, you need to think about how many files you need to bundle together to pass to your friends) Information hiding: Users need not go into details of the object before using the object (E.g., you don’t need to know the circuit of a TV set if you want to watch TV) Safety: Users of the object may not directly access the internal state of the object. This reduces the possibility of erroneous situations. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Library Reusability

6 Objects of class button What is a Class? A Class is a blueprint or prototype that defines the variables and methods common to all objects of a certain kind Every object of a Class is an instance of that class Benefit - Reusability This arrangement saves effort in developing a number of objects of the same kind. Usually objects of a class are used many times in an application Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

7 Variables Color (e.g. Grey ) Size (e.g. 2cm x 2cm ) Shape (e.g. Rectangular ) (protruded) Method The Button Class: Instance of Button Class Instantiation Press( ) Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

8 5.2 Objects and Classes in C++ Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

9 To declare a class, use the class keyword as follows Declaring this class does NOT allocate memory for a Cat. Only tell the compiler what a Cat is, how big a Cat is (by member variables, e.g. itsAge, itsWeight ), what a Cat would do (by member functions, e.g. Meow() ). Declaration of classes should be placed in the header file and included into your program. Declaring Classes in C++ class Cat { unsigned intitsAge; // Member variable unsigned intitsWeight;// Member variable void Meow(); // Member function }; Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ #include Definition of Meow() should follow somewhere P.12

10 When a class is defined, we can further define the objects of that class: Cat Frisky;// define one of the Cats call Frisky It states that we are going to handle a Cat called Frisky. It is similar to declaring a number of the type integer int xyz;// define one of the integers call xyz Obviously, if one declares two cats as follows Cat Frisky, Felix;// define two Cats Frisky is never equal to Felix, although they both belong to the class Cat, i.e. they are cats. Declaring an Object of a Class Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

11 When an object is defined, we can access the members of that object based on its class definition. For example, if we want to know the weight of Frisky : unsigned int weight = Frisky.itsWeight; // Get the weight of Frisky The operator ‘. ’ allows us to access the members of the object. Similarly, if we want to know the age of Frisky : unsigned int age = Frisky.itsAge; // Get the age of Frisky If we want to ask Frisky to meow : Frisky.Meow(); // Ask Frisky to execute meow() Accessing Class Members Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Variable and methods

12 Never access directly to class Cat.itsAge = 5;// Don’t do that! Cat is class It is because Cat is only a template of all cats. A different cat may have a different age. If we want to assign the age of Frisky, we write: Frisky.itsAge = 5;// Set the age of Frisky to 5 Also, if the class doesn’t define a member, you cannot use it. Frisky.bark(); // Frisky has no bark() defined Frisky.itsColor = 5;// Also error, because the class // Cat does not have itsColor Accessing Class Members Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ P.9

13 Members can be divided into public members or private members. Private members of a class are those members that can only be accessed by methods of that class. By default, all members are private. Public members of a class are those members that can be accessed by other class objects and functions. This mechanism provides the privacy or security to the information of a class that requires protection. Private Versus Public Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Typically, access private members by public methods.

14 #include // for cout using namespace std; class Cat// declare the class { int itsAge; int itsWeight; }; int main() { Cat Frisky; Frisky.itsAge = 5;// assign to the member variable cout << "Frisky is a cat who is "; cout << Frisky.itsAge << " years old.\n"; return 0; } Private Versus Public An error is generated since by default itsWeight and itsAge are private. They cannot be accessed even by main(). Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

15 Private Versus Public #include // for cout using namespace std; class Cat// declare the class { public: int itsAge; int itsWeight; }; int main() { Cat Frisky; Frisky.itsAge = 5;// assign to the member variable cout << "Frisky is a cat who is "; cout << Frisky.itsAge << " years old.\n"; return 0; } Now itsWeight and itsAge are public members. They can be accessed by main(). Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

16 How Private Variables Are Used? #include // for cout using namespace std; class Cat// declare the class Cat { public: void SetAge (int age); int GetAge(); void SetWeight (int weight); int GetWeight(); private: int itsAge; int itsWeight; }; int Cat::GetAge() {return itsAge; } void Cat::SetAge(int age) {itsAge = age; } Public Accessor Methods Accessor methods provide access to private members since they are in the same class. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Read/write Private member variables P.18 P.21

17 Implementing Class Methods Member functions declared in a class are only prototypes of these functions. Actual implementation (the operations performed by the function) should be separately described. #include // for cout using namespace std; class Cat// declare the class Cat { public: int GetAge(); private: int itsAge; }; int Cat::GetAge() {return itsAge; } To indicate GetAge() is a function of class Cat. That’s the implementation Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Publicly known Only the developer knows

18 Exercise 5.2a Based on the program in page 16, write a program that will first print the age and weight of a cat called Felix, and then it asks the user to input the age and weight of that cat. To do that, you need to do the following things: a.Complete the implementation of the member functions GetWeight() and SetWeight(). b.Add the main() function that prints the current status of a cat Felix and ask for user’s input to modify the status. c. Build the result program and note the result. d. Before you ask the user to input the age and weight, what are their initial values? Are they the ones you expect? Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Age and weight Use run-time debugger P.16

19 Constructors and Destructors How do we initialize an object?  By means of the constructor of the class Every class should have a constructor. User can define its own constructor for the class. Otherwise, the compiler will make one for the user although it does nothing. Constructor is a function of which the compiler will call if an object of this class is constructed (created). Besides constructor, every class has also a destructor that will be called when the object of that class is destructed (removed). Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ In memory

20 class Cat { public: Cat(int initialAge);// Constructor of Cat ~Cat();// Destructor of Cat int GetAge(); void SetAge(int Age); void Meow(); //public function private: int itsAge; }; Cat::Cat (int initialAge) { itsAge = initialAge; } Cat::~Cat() { } void Cat::Meow() {} A typical Class definition with user- defined constructor and destructor Implementation of constructor When any object of the class Cat is constructed, this function is called and in effect it will set itsAge to the parameter passed. Implementation of constructor When any object of the class Cat is constructed, this function is called and in effect it will set itsAge to the parameter passed. Implementation of destructor When any object of the class Cat is destructed, this function is called which in effect does nothing. Implementation of destructor When any object of the class Cat is destructed, this function is called which in effect does nothing. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

21 Constructors and Destructors A possible main() function for the class Cat above is as follows: // some lines of p.20 // some lines of p.16 int main() {Cat Frisky(5); Frisky.Meow(); cout << "Frisky is a cat who is "; cout << Frisky.GetAge() << " years old.\n"; Frisky.Meow(); //Meow() does nothing here Frisky.SetAge(7); cout << "Now Frisky is "; cout << Frisky.GetAge() << " years old.\n"; Frisky.Meow(); return 0; } A Cat Frisky is constructed here The constructor is called and the parameter 5 is passed to the constructor and in turn initializes the private member variable itsAge to 5. A Cat Frisky is constructed here The constructor is called and the parameter 5 is passed to the constructor and in turn initializes the private member variable itsAge to 5. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

22 It is possible that some functions will never change the value of any member. It is desirable to declare them as const member function. Then, the compiler will automatically check if there is any inconsistency in the program. It helps to debug the program. In the example above, obviously GetAge() and GetWeight() will not change any member Hence they can be declared as const as follows: int GetAge() const;// add the keyword const when int GetWeight() const;// you declare the functions const Member Functions Computer Programming and Basic Software Engineering 5. Objects and Classes in C++ Read only

23 Exercise 5.2b From the program you wrote in exercise 5.2a, a.Add the constructor and destructor such that the initial age and weight of Felix is 5 and 10 respectively. b.Use the const keyword to define the class methods that will not modify the member variables. Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

24 Exercise 5.2c Identify the errors in the following program. The program is divided into 3 parts: class declaration, member functions implementation and the main(). Fix the errors and verify your results by building the program. #include // for cout using namespace std; class Cat// begin declaration of the class { public:// begin public section Cat(int initialAge);// constructor ~Cat();// destructor int GetAge() const;// accessor function void SetAge(int Age);// accessor function void Meow();// general function private: int itsAge;// member variable }; Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

25 Cat::Cat(int initialAge) { itsAge = initialAge; cout << "Cat Constructor\n"; } Cat::~Cat() { cout << "Cat Destructor\n"; } int Cat::GetAge() { return (itsAge++); } void Cat::SetAge(int age) { itsAge = age; } void Cat::Meow() { cout << "Meow.\n"; } int main() {Cat Frisky; Frisky.Meow(); Frisky.Bark(); Frisky.itsAge = 7; return 0; } Computer Programming and Basic Software Engineering 5. Objects and Classes in C++

26 Acknowledgment The slides are based on the set developed by Dr. Frank Leung (EIE).