0% found this document useful (0 votes)
6 views

Introduction to Classes and Objects

The document provides an introduction to classes and objects in C++, detailing course information, evaluation criteria, and fundamental concepts of object-oriented programming. It explains class definitions, visibility labels, member functions, and the use of accessor and mutator functions, along with UML diagrams for visual representation. An example of a Rectangle class is included to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Introduction to Classes and Objects

The document provides an introduction to classes and objects in C++, detailing course information, evaluation criteria, and fundamental concepts of object-oriented programming. It explains class definitions, visibility labels, member functions, and the use of accessor and mutator functions, along with UML diagrams for visual representation. An example of a Rectangle class is included to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Introduction to Classes and

Objects
Course General Information

– Office: Room 512


– Email: [email protected]
– Office hours Monday and Wednesday: 14:00 p.m
to 15:00 p.m
• Course managing style:
– Lecture & Lab class: covered by me
Course General Information

Quiz Project Practical


(Attendance) 5% 15% 10% 10%
Assignment

Evaluation
Criteria

Class (Midterm (Final


30% 30% Total = 100 %
Participation exam) Exam)

Methods of Assessment will be made based on the midterm exam (30%), final exam (30%), practical
Evaluation Assignment (10%), Quizzes (15%), Class Attendance (5%) and Project(10%)
Four Basic Properties of Object Oriented
Programming
Classes in C++
• A class definition begins with the keyword
class.
• The body of the class is contained within a set
of braces, { } ; (notice the semi-colon).

class class_name Any valid


{ identifier
….
….
Class body (data member
….
+ methods)
};
Basics for Class

1. The class body contains the declaration of variables and functions.

2. These functions and variables collectively called class members.

3. They are usually grouped under two sections , namely private and
public to denote which of the members are private and which of them
are public.

4. The keyword private and public are known as visibility labels.

5. Note that these keywords are followed by a colon.


C++ Class Definitions:
When you define a class, you define a blueprint
for a data type. This doesn't actually define any
data, but it does define what the class name
means, that is, what an object of the class will
consist of and what operations can be performed
on such an object.
For example, we defined the Box data type using
the keyword class as follows:
Define C++ Objects:

A class provides the blueprints for objects, so basically


an object is created from a class. We declare objects
of a class with exactly the same sort of declaration
that we declare variables of basic types.
Following statements declare two objects of class Box:

Both of the objects Box1 and Box2 will have their own
copy of data members.
Private and Public Visibility Labels

1. The class members that have been declared as private can be


accessed only from within the class
2. Public members can be accessed from outside the class also.
3. The data hiding(using private declaration)is the key feature of OOP.
4. The use of keyword private is optional.
5. By default , the members of class are private.
6. The variables declared inside the class are known as data members
and functions are known as member functions.
7. Only the member functions can have access to the private class
members.
8. However, the public members(both data and functions) can be
accessed from outside the class.
Defining Member Functions

Member functions can be defined in two places:

1. Outside the class definition


2. Inside the class definition
Outside the Class Definition

An important difference between a member function and a normal


function is that a member function incorporates a membership ‘identity
label’ in the header.
This label tells the compiler which class the function belongs to.
Return-type class-name :: function-name (argument declaration)
{

Function Body

}
A C++ Program with Class
Private Member Function
A private member function can only be called by another
function that is a member of its class. Even an object cannot
invoke a private function using dot operator.
set Functions and get Functions

Software engineering with set and get functions

1. public member functions that allow clients of a class to


set or get the values of private data members
2. set functions sometimes called mutators and get
functions sometimes called accessors
3. Allows the creator of the class to control how clients
access private data
4. Should also be used by other member functions of the
same class
Accessor and Mutator Function
The Unified Modeling Language
• UML stands for Unified Modeling Language.

• The UML provides a set of standard diagrams


for graphically depicting object-oriented
systems
UML Class Diagram

• A UML diagram for a class has three main


sections.

CS1 Lesson 13 -- Introduction to Clas 20


ses
Example: A Rectangle Class
class Rectangle
{
private:
double width;
double length;
public:
bool setWidth(double);
bool setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
UML Access Specification Notation

• In UML you indicate a private member with


a minus (-) and a public member with a
plus(+).
These member variables are
private.

These member functions are


public.
UML Data Type Notation

• To indicate the data type of a member variable,


place a colon followed by the name of the data type
after the name of the variable.

- width : double
- length : double
UML Parameter Type Notation
• To indicate the data type of a function’s
parameter variable, place a colon followed by
the name of the data type after the name of
the variable.

+setWidth(w : double)
UML Function Return Type Notation

• To indicate the data type of a function’s return


value, place a colon followed by the name of
the data type after the function’s parameter
list.
+ setWidth(w : double) : void

25
The Rectangle Class

You might also like