110 likes | 119 Views
Learn about classes as blueprints for objects, access specifiers like private, public, protected, and the basics of objects in OOP.
E N D
Table of Contents • Class • Objects
Class • In object-oriented programming, a class is a construct that is used as a blueprint to create instances of the class (class instances, class objects, instance objects or just objects). A class defines constituent members which enable class instances to have state and behavior. Data field members (member variables or instance variables) enable a class object to maintain state. Other kinds of members, especially methods, enable a class object's behavior. Class instances are of the type of the associated class. For example, an instance of the class "Fruit" (a "Fruit" object) would be of the type "Fruit". A class usually represents a noun, such as a person, place or (possibly quite abstract) thing. Programming languages that include classes as a programming construct subtly differ in their support for various class-related features. Most support various forms of class inheritance. Many languages also support advanced encapsulation control features, such as access specifiers.
Syntax of Class Class bca { Private : data members; data members; Public: member Functions() { Coding part; } }; // end of class definition Void main() { Class bca b; // b is the object of class bca b. Member functions(); // calling the member functions through object i.e b getch(); }
Access Specifiers • A common set of access specifiers that many object-oriented languages support is: • Private • Public • Protected
Private • The private keyword can be used within a class definition (followed by a colon) to mark subsequent member declarations as private or before a base-class name in the class header to mark the inheritance as private. The default access level of a class is private, both for members and base classes. • Private members can be used by the class itself or by friends. • Private inheritance means all public and protected members in the base class are private to the derived class.
Public • The public keyword can be used within a class definition (followed by a colon) to mark subsequent member declarations as public or before a base-class name in the class header to mark the inheritance as public. The default access level of a struct is public, both for members and base classes. • Public members can be used freely by any other class or function. • Public inheritance means all members of the base class retain their accessibility levels.
Protected • Protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else. You use the protected access level when it is appropriate for a class's subclasses to have access to the method or field, but not for unrelated classes.
Objects • Objects are the basic run time entities that may represent a person ,place, a bank account and any other thing . • Objects" are the foundation of object-oriented programming, and are fundamental data types in object-oriented programming languages. • In a language where each object is created from a class, an object is called an instance of that class. If each object has a type, two objects with the same class would have the same data type. Creating an instance of a class is sometimes referred to as instantiating the class.
Objects • A real-world example of an object would be “a flying bird", which is an instance of a type (a class) called “Bird“.
Properties of an Object • Identity: the property of an object that distinguishes it from other objects. • State: describes the data stored in the object. • Behavior: describes the methods in the object's interface by which the object can be used.
Properties of an Object • Identity: the property of an object that distinguishes it from other objects. • State: describes the data stored in the object. • Behavior: describes the methods in the object's interface by which the object can be used.