Successfully reported this slideshow.
Your SlideShare is downloading. ×

[OOP - Lec 06] Classes and Objects

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 17 Ad
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

Similar to [OOP - Lec 06] Classes and Objects (20)

More from Muhammad Hammad Waseem (18)

Advertisement

[OOP - Lec 06] Classes and Objects

  1. 1. Classes and Objects Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  2. 2. Introduction • An object has the same relationship to a class that a variable has to a data type. • An object is said to be an instance of a class, in the same way • my 1954 Chevrolet is an instance of a vehicle. • A class is a template to create many similar objects. • As my BMW and Chevrolet is an instance of vehicle class.
  3. 3. Objects • An object can be a variable, a data structure, or a function. • In the class-based object-oriented programming paradigm, "object" refers to a particular instance of a class • where the object can be a combination of • variables, • functions, and • data structures.
  4. 4. Classes • A class is a blueprint for creating many similar objects. • The created object is an instance of that class. • Objects created from the same class will have the same basic structure and functionality. • All cars created from the same Ford Escort blueprints will look and work basically the same. • Many instances can be created from a single class. • Just as many Ford Escorts can be created from the same set of Ford Escort blueprints.
  5. 5. Classes and Instances
  6. 6. Data Elements of Class • A class may contain attributes and behaviors for a particular object. • In OOP, these are called as: • Data members/ instance variables (attributes) • Member functions/ instance methods/ functions/ methods (behaviors/actions)
  7. 7. Instance Variables • An instance variable (attribute) of an object is a piece of information attached to an instance (object). • The name of a Person object, the model and year of a Car object, etc. • The instance variables that an object has are defined in the object's class: • An object can usually have many instance variables, of many different types. • Each object is given its own private space to hold its instance variables. • Assigning a new value to an instance variable of one object does not affect the instance variables of any other object.
  8. 8. Instance Methods • When we define objects, we usually have an idea of what we want to do with them... • – I'm dealing with Person objects in an employee database... I want to be able to ask each Person object their name, weight, and age. • – I'm dealing with Car objects in a driving simulation... I want to be able to start a Car, change its speed, turn its steering wheel, etc. • An action that involves a single object is usually implemented as a special kind of function/subroutine attached to that object's class, called an instance method (or, more commonly, just a method).
  9. 9. Defining the Class • Here’s the definition (sometimes called a specifier) for the class: class ClassName //define a class { private: //Access Specifier Data Members; //class data public: Member Functions //class data };
  10. 10. Defining a Class: ClassName • The definition starts with the keyword class, followed by the class name. • Like a structure, the body of the class is delimited by braces and terminated by a semicolon. (Don’t forget the semicolon). • Remember, • Data constructs such as structures and classes end with a semicolon, • While control constructs such as functions and loops do not.
  11. 11. Defining a Class: Access Specifiers • The body of the class contains two unfamiliar keywords: • private and • public. • A key feature of object-oriented programming is data hiding. • This term does not refer to the activities of particularly paranoid programmers; • rather it means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class. • The primary mechanism for hiding data is to put it in a class and make it private. • Private data or functions can only be accessed from within the class. • Public data or functions, on the other hand, are accessible from outside the class.
  12. 12. Defining a Class: Class Data • The data items within a class are called data members (or sometimes member data). • There can be any number of data members in a class, just as there can be any number of data items in a structure. • The data member follows the keyword private, so it can be accessed from within the class, but not from outside.
  13. 13. Defining a Class: Member Functions • Member functions are functions that are included within a class. • (In some object-oriented languages, such as Smalltalk, member functions are called methods; some writers use this term in C++ as well.) • The function bodies of these functions have been written on the same line as the braces that delimit them. You could also use the more traditional format for these function definitions. • However, when member functions are small, it is common to compress their definitions this way to save space. • Functions follow the keyword public, they can be accessed from outside the class.
  14. 14. Defining a Class: Functions Are Public, Data Is Private • Usually the data within a class is private and the functions are public. • The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed from outside the class. • However, there is no rule that says data must be private and functions public; in some circumstances you may find you’ll need to use private functions and public data.
  15. 15. Defining a Class: Member Functions Within Class Definition • The member functions definitions may be done within the class definition. • Member functions defined inside a class this way are created as inline functions by default. • We’ll see later that it is also possible to declare a function within a class but to define it elsewhere. • Functions defined outside the class are not normally inline.
  16. 16. Defining Objects • Class only describes how they will look when they are created, just as a structure definition describes how a structure will look but doesn’t create any structure variables. • It is objects that participate in program operations. • Defining an object is similar to defining a variable of any data type • When an object of a class is created, the space for all data members defined in the class is allocated in the memory according to their data types. • An object is also known as instance. • The process of creating an object of a class is also called instantiation. • Syntax: ClassName ObjectName;
  17. 17. Defining Objects: Executing Member Functions • An object of a particular class contains all data members as well as member functions defined in that class • The data members contains the value related to the object. • The functions are used to manipulate data members. • The member functions can be executed only after creating an object. • Syntax: ObjectName.memberfunction();

×