Object Oriented Programming Using C++: Structures and Classes
Object Oriented Programming Using C++: Structures and Classes
using C++
STRUCTURES and
CLASSES
LECTURE-06
1
STRUCTURES
2
WHAT IS A STRUCTURE?
A structure is an aggregate data structure used to keep
different pieces of information together as a single data
record
4
Structures
Structures hold data that belong together.
Examples:
Student record: student id, name, major,
gender, start year, …
Bank account: account number, name,
currency, balance, …
Address book: name, address,
telephone number, …
In database applications, structures are
5
called records.
struct basics
Example:
struct Date { The “Date” structure
int day;
int month; has 3 members,
int year; day, month & year.
} ;
6
ACCESSING COMPONENTS
The members of a struct type variable are accessed with the dot (.) operator:
<struct-variable>.<member_name>;
• Use the name of the record
the name of the member
separated by a dot .
student.Id = 5;
cout << student.Id;
• The dot is called the member selector
7
STRUCT BASICS
Name Name
Student1Id Gender Id Gender Student2
Dept Dept
For example:
student1 = student2;
9
EXAMPLE
#include int main()
<iostream> { cout << " Manufacture \n";
using namespace Date manufact; cout << manufact.month
std; << “-" << manufact.day
Date expiration;
<<“- " <<
struct Date manufact.year<<endl;
{ manufact.month = 10;
int month; manufact.day = 20; cout << "Expiration \n";
int day; manufact.year = 2007; cout << expiration.month
int year; << “-" << expiration.day
}; expiration = manufact; <<“-" <<
expiration.year = expiration.year<<endl;
2009;
} 10
MORE ON STRUCTURES
struct Person
{
Date birthday;
int ID;
}
Person teacher;
teacher.birthday.month = 1;
teacher.ID = 152; 11
AGGREGATE OPERATIONS WITH
STRUCTURES
no I/O
cout << student1;
cin >> student1;
no comparisons
if (student1 < student2)
cout << ...; 12
AGGREGATE OPERATIONS WITH
STRUCTURES
13
ARRAYS OF STRUCTURES
0 1 2 … 98 99
An array of structs: Multiple types of data in
each array element.
0 1 2 … 98 99 14
ARRAYS OF RECORDS
16
CLASSES
17
CLASSES
A class is similar to a struct
A class is a specification of a group of objects that all
have the same essential properties
A variable of class type is known as object and the
operations on that type are called methods
Objects are made from classes, similar to the way that
objects are made from structs
18
CLASS DECLARATION
Syntax
class ClassName
{
public:
Declarations of public
members
private:
Declarations of private
members
}; 19
CLASS BASICS
Simple class (Time)
Private and public
Data is private, functions are public
Data hiding (hidden from whom)
Protects unintentional mistakes (not security)
Defining objects
Calling member functions & accessing data
Objects as physical objects (car in a game, circle
on screen, bank account (each account is object
20
of ACCOUNT class)
A CLASS EXAMPLE
class
CheckBook
Properties (data members)
balance, lastCheck, lastDeposit
Operations (methods)
setBalance
writeCheck
deposit
getBalance
21
AN OBJECT IS AN INSTANCE OF A
CLASS
For example:
object
checkBook1
Balance: 50,000
Last check: 35,000
Last deposit: 20,500
22
HOW THE MAIN PROGRAM USES A CLASS
OBJECT
The main program does not access the data
within a class object, usually
The main program only accesses the functions
of a class object
communication occurs by passing data as parameters
into the object’s function
the object passes data to the main program through its
return type
23
MAIN PROGRAM AND
OBJECT
Object
public: private:
functions data
Main
Program
24
MAIN PROGRAM CALLS A FUNCTION IN
THE OBJECT
Object
public: private:
functions data
Main
Program
25
THE FUNCTION ACCESSES
DATA
Object
public: private:
functions data
Main
Program
26
FUNCTION RETURNS A VALUE
BACK TO THE MAIN PROGRAM
Object
public: private:
functions data
Main
Program
27