130 likes | 146 Views
CITA 342 Section 2. Object Oriented Programming (OOP). Object Oriented Programming and Classes. Classes form the basis of object-oriented programming Object-oriented programming is a way of designing and accessing code
E N D
CITA 342 Section 2 Object Oriented Programming (OOP)
Object Oriented Programming and Classes • Classes form the basis of object-oriented programming • Object-oriented programming is a way of designing and accessing code • The pieces of the programming puzzle—data types, variables, control structures, functions, and so on—are the same as in any other type of programming
Classes • In C++ programming, classes are data structures that contain variables along with functions for manipulating the variables • The functions and variables defined in a class are called class members • Class variables are referred to as data members or member variables, whereas class functions are referred to as member functions or function members
Classes • To use the variables and functions in a class, you declare an object from that class • When you declare an object from a class, you are said to be instantiating an object • You can work with a class as a single unit, or object, in the same way you work with a variable
Creating Classes with the class Keyword • The most important type of class used in C++ programming is defined using the class keyword • For brevity, classes defined with the class keyword will be referred to simply as classes • When you use a period to access an object’s members, the period is referred to as the member selection operator • You use the member selection operator to initialize or modify the value stored in an object field
Access Specifiers • You can set access specifiers for class members • Access specifiers control a client’s access to individual data members and member functions • There are four levels of access specifiers: public, private, protected, and friend • The public access specifier allows anyone to call a class’s member function or to modify a data member
Access Specifiers • The private access specifier prevents clients from calling member functions or accessing data members • Both public and private access specifiers have what is called class scope • You place access specifiers in a class definition on a single line followed by a colon
Member Functions • Member functions are usually declared as public, but they can also be declared as private. public member functions can be called by anyone, whereas private member functions can be called only by other member functions in the same class • In order for your class to identify which functions in an implementation file belong to it, you precede the function name in the function definition header with the class name and the scope resolution operator (::)
Constructor Functions • A constructor function is a special function with the same name as its class that is called automatically when an object from a class is instantiated
friend Functions and Classes • When you use the public access modifier with a class member, the entire world has access to that class member • In contrast, only members of the same class can access private class members • The friend access modifier allows designated functions or classes to access a class’s private members • Only a class itself can designate the function and class friends that can access its private members; external functions and classes cannot make themselves friends of a class
Base Classes and Derived Classes • Inheritance is the ability of one class to take on the characteristics of another class • When you write a new class that inherits the characteristics of another class, you are said to be deriving or subclassing a class • An inherited class is referred to as the base class, and the class that inherits a base class is referred to as a derived class
Base Classes and Derived Classes • When a class is derived from a base class, the derived class inherits all of the base class’s data members and all of its member functions, with a few exceptions such as the Constructor functions
Access Specifiers and Inheritance • Even though a derived class inherits the class members of a base class, the base class’s members are still bound by its access specifiers • The protected access specifier restricts class member access to the class itself, to member functions in classes derived from the class, or to friend functions and friend classes