Classes (a user-defined data type) are the basic unit of programming Attributes are called “data members” (variables) Behaviors are called “member functions” Objects are instantiated (created) from the classes
for the input/output stream functions See link to C++ Standard Library header files For programmer-defined header files, use double quotes around the name of the .h file: #include "myHeaderFile.h" #include <iostream>
code at the top of our program, we have to wrote “std::” in front of each standard library reserve word (std::cout instead of cout) In multiple-file programs, namespace is used to avoid naming conflicts using namespace std;
Defines the data members (variables) and member functions of the class The class instantiates (creates) objects that store data and that also use these functions See example code at: fraction.cpp
declared below public are accessible anywhere in the program 2. private: Data members and member functions declared below private are only accessible to member functions of the class 3. protected: Used for inheritance
more privilege than necessary to perform a job, such as accessing functions and variables Basically, if you don’t need access, you don’t get access This is an important concept in software engineering, computer security, and other fields
has less bugs For example, access to private data members is restricted to member functions, which use the data members in appropriate ways (information hiding) Less likely a user can crash a program For example, for the Fraction class, the user cannot set the denominator to zero (0)
object- oriented languages to hide the details of how a class works by restricting access to private data members, which can only be manipulated by member functions Encapsulation is grouping the data members and member functions together into a single class
curly brackets, and semicolon 2. Private data members at bottom by convention 3. Public constructor to initialize the data members 4. Public display function to output the data members 5. Driver (main) function with objects and function calls to test the constructor and display function
data members numerator and denominator, a public constructor and print() function, and a main function as the driver program Two restrictions: The denominator cannot be zero: illegal fraction with undefined value The denominator is always positive by convention
class definition (information hiding) Cannot be initialized (only in the constructor) Start with a lowercase letter Data member syntax: dataType variableName;
public section of the class definition Starts with a capital letter Constructor syntax: ClassName(dataType parameters){ //code to initialize data members }
section of the class definition All functions start with a lowercase letter Exception is constructors We are only displaying the data members, so we don’t want to change them (Principle of Least Privilege)
the function header, so we cannot change the data members by mistake (Principle of Least Privilege) Output function syntax: returnType function (dataType parameters) const{ //code to display data members that cannot be changed }
need to instantiate (create) objects and call the display function This is done in the main() function Syntax for instantiating objects: ClassName objectName; Syntax for calling a function: objectName.functionName(parameters);
that changes the values stored in the data members Usually starts with “set” Mutator function syntax: void setDataMember(dataType parameters){ dataMember = parameter; }
that returns the value of a data member Usually starts with “get” Accessor function syntax: returnType getDataMember () const{ return dataMember; }
functions are private Also called a “helper function” Supports the operation of member functions Not intended to be used by the clients of a class For example, cannot use in the main() function
the class that are usually public, constant, and in all capital letters Public, so can be accessed outside the class in the driver program Constant, so uses the Principle of Least Privilege Have to be initialized when declared
const dataType VARIABLE_NAME = value; Static data member access syntax: variable = ClassName::VARIABLE_NAME; variable = object.VARIABLE_NAME; See example program at: functions.cpp