Introduction To Classes
Introduction To Classes
Introduction to Classes
class Gradebook {
public:
void displayMessage() {
class cout << "Welcome to the grade book” << endl;
}
};
int main(){
main Gradebook myGradeBook;
function myGradeBook.displayMessage();
}
Class
• A class can be visualized as a three-compartment box, as
illustrated:
• Classname (or identifier): identifies the class.
• Data Members or Variables (or attributes, states, fields): contains
the static attributes of the class.
• Member Functions (or methods, behaviors, operations): contains
the dynamic operations of the class.
class class_name
{
access_specifier_1:
member1;
access_specifier_2:
member2;
...
};
Access-specifier
• access_specifier modifies the access rights for the members:
• public members are accessible from anywhere where the
object is visible.
• private members of a class are accessible only from within
other members of the same class.
• protected members are accessible from other members of the
same class but also from members of their derived classes.
• As a rule of thumb,
• data members should be declared private
• member functions should be declared public.
• By default, members would be assumed as private.
Class definition (Example)
class Circle { // class name
private:
double radius; // Data members (variables)
string color;
public:
double getRadius(); // Member functions
double getArea();
}
void Gradebook::displayMessage() {
cout << "Welcome to the grade book!" << endl;
}
Class object declaration
• A class variable is called a class object or class instance; or simply called
object. All the instances of a class have similar properties, as described in
the class definition.
• Typically, you cannot call a member function of a class until you create an
object of that class.
We can store the grades for two courses by creating two objects
“course1” and “course2”.
nameOfCourse = nameOfCourse =
data members “Course 1: KIE1004” “Course 2: KIE1005”
grade = {3.5, 3.7, …} grade = {3.9, 2.5, …}
getCourseName() getCourseName()
member functions printGrade() printGrade()
Accessing class members
• General syntax for an object to access a member of a class is:
classObject.memberName
// display a welcome message to the GradeBook user Use set and get functions,
void displayMessage() { even within the class
// call getCourseName to get the courseName
cout << "Welcome to the grade book for\n" << getCourseName()
<< "!" << endl;
}
private:
string courseName; // course name for this GradeBook
};
private members accessible only to
member functions of the class
End of class body
Example – Gradebook project(3/3)
// function main begins program execution
int main() {
// create two Gradebook objects
Gradebook course1("CS101 Introduction to C++ Programming");
Gradebook course2("CS102 Data Structures in C++");
Constructor
# sign: protected member
Placing a class in a separate file
• .cpp file is known as a source-code file
• Header files (.h)
• Separate files in which class definitions are placed
• Allow compiler to recognize the classes when used elsewhere
• Generally have .h filename extensions
• can be included in any program to reuse the class
• Programs use #include pre-processing directives to include
header files
• Pre-processor first looks in current directory
• If the file is not found, looks in C++ Standard Library directory
• To ensure that the preprocessor can locate header files correctly,
#include preprocessor directives should place the names of user-
defined header files in quotes (e.g., "GradeBook.h") and place
the names of C++ Standard Library header files in angle brackets
(e.g., <iostream>).
Placing a class in a separate file
• For better software engineering, it is recommended that the class
declaration and implementation be kept in 2 separate files: declaration is a
header file ".h"; while implementation in a ".cpp".
#include <iostream> #include <iostream>
using namespace std; #include “GradeBook.h”
using namespace std;
class Gradebook {
public: int main(){
void displayMessage() { Gradebook myGradeBook;
cout << "Welcome to the grade myGradeBook.displayMessage();
book” << endl; }
}
};
GradeBook.h main.cpp