CLASSES AND OBJECTS In objectoriented programming languages like
CLASSES AND OBJECTS In object-oriented programming languages like C++, the data and functions (procedures to manipulate the data) are bundled together as a self-contained unit called an object. A class is an extended concept similar to that of structure in C programming language; this class describes the data properties alone. In C++ programming language, a class describes both the properties (data) and behaviors (functions) of objects. Classes are not objects, but they are used to instantiate objects. q
CLASSES A Class is a user defined data-type which has data members and member functions. Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class. In the above example of class Car, the data member will be speed limit, mileage etc and member functions can be apply brakes, increase speed etc.
objects q An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i. e. an object is created) memory is allocated. Defining Class and Declaring Objects A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end. classes-and-objects-in-c Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects. Syntax: Class. Name Object. Name;
How to access data member and member function in C++? You can access the data members and member functions by using a. (dot) operator. For example, o 2. function 1(); This will call the function 1() function inside the Test class for objects o 2. Similarly, the data member can be accessed as: o 1. data 2 = 5. 5; It is important to note that, the private members can be accessed only from inside the class. So, you can use o 2. function 1(); from any function or class in the above example. However, the code o 1. data 2 = 5. 5; should always be inside the class Test.
- Slides: 6