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

Object Oriented Programming Using C++: Structures and Classes

Structures and classes allow programmers to organize related data and functions together. A structure defines a record with different member data types, while a class defines objects with both data fields and methods. An array of structures can hold multiple records of different types. Class objects encapsulate private data that is accessed through public member functions, hiding implementation details and protecting data. The main program interacts with class objects by calling member functions that may access private data and return values.

Uploaded by

AK
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Object Oriented Programming Using C++: Structures and Classes

Structures and classes allow programmers to organize related data and functions together. A structure defines a record with different member data types, while a class defines objects with both data fields and methods. An array of structures can hold multiple records of different types. Class objects encapsulate private data that is accessed through public member functions, hiding implementation details and protecting data. The main program interacts with class objects by calling member functions that may access private data and return values.

Uploaded by

AK
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Object Oriented Programming

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

A structure is like an array except that each element can


have a different data type, and the elements in a structure
have names instead of subscript values

Consider storing students’ data: name, age, batch,


address, city, CGPA
3
STRUCTURES
A struct holds data, like an array
Each unit of data in a struct is called a data member (or
member)
they are called “elements” in arrays
In a struct, each data member can have a different data
type
in arrays, the data type of each element is the same

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

 Declaration of a variable of struct type:


Example:
StudentRecord Student1, Student2;

Name Name
Student1Id Gender Id Gender Student2

Dept Dept

Student1 and Student2 are variables of StudentRecord 8


type.
STRUCT ASSIGNMENT

The values contained in one struct type


variable can be assigned to another variable of
the same struct type.

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

Structures can be hierarchical;


a member variable can be a structure itself:

struct Person
{
Date birthday;
int ID;
}
Person teacher;
teacher.birthday.month = 1;
teacher.ID = 152; 11
AGGREGATE OPERATIONS WITH
STRUCTURES

Limitations on aggregate operations

no I/O
cout << student1;
cin >> student1;

no arithmetic operations


student3 = student1 + student2;

no comparisons
if (student1 < student2)
cout << ...; 12
AGGREGATE OPERATIONS WITH
STRUCTURES

 struct variables must be compared member-wise.


 To compare the values of student and newStudent, you
must compare them member-wise, as follows:

if(student1.firstName == student2.firstName &&


student1.lastName == student2.lastName) ...

13
ARRAYS OF STRUCTURES

An ordinary array: One type of data

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

First declare a struct (such as student)


Then specify an array of that type
Student stlist [50];

Access elements of the array, then members of the struct

How do we for (x = 0; x <50; x++)


print all the cout << stlist[x].name ;
name fields? 15
STRUCT VARIABLES AND
FUNCTIONS

 A struct variable can be passed as a parameter either


by value or by reference.
A function can return a value of the type struct

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

You might also like