0% found this document useful (0 votes)
2 views6 pages

Lecture No 10

The document explains the concept of objects in programming, specifically in C++, describing them as instances of data types with associated values and operations. It introduces structures and unions as ways to create user-defined data types that can encapsulate multiple properties, using the example of a Book structure. Additionally, it covers enumerated data types and the typedef keyword for creating synonyms for existing data types.

Uploaded by

mesaad074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Lecture No 10

The document explains the concept of objects in programming, specifically in C++, describing them as instances of data types with associated values and operations. It introduces structures and unions as ways to create user-defined data types that can encapsulate multiple properties, using the example of a Book structure. Additionally, it covers enumerated data types and the typedef keyword for creating synonyms for existing data types.

Uploaded by

mesaad074
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is an Object

• Object can be anything you like


• More specifically, it is an instance of a
Programming Fundamentals data type
• For example;
int dollars = 0;
Structures
• Here we have defined a single instance
of type int and given the name dollars
• Variable dollars is an object of type int

Programming Fundamentals– Structures & Unions 2

What is an Object Creating our own Data Types

• An object has a value – the integer • In C++ we can define our own data
dollars has the value 0 types and specify the operations that
can be applied to them
• An object also have a set of operations
like the object dollars has a set of • The types that we defined are referred
to as user defined data types
operations that we can apply –
arithmetic (+, -) and logical operations • These are structures and unions; we
use keywords struct and union
(<, >, ==) respectively to create them

Programming Fundamentals– Structures & Unions 3 Programming Fundamentals– Structures & Unions 4

Understanding Structures Understanding Structures

• The variables and data types that we have • For example a Book has a
seen have consisted of a single entity – a – Title, Author, Publisher, Date of Publication,
numeric of some kind, or a character of a – Number of Pages, Price, Accession Number,
string • To identify a book in our program, we could
• In order to describe everything in the world, specify separate variables, one to contain each
we need to define several values that are of these properties
usually of a number of different types • Ideally we should have a single data type called
• From this idea comes the notion of structures Book – which embodies all of these properties
• This is exactly that a structure can do for us
Programming Fundamentals– Structures & Unions 5 Programming Fundamentals– Structures & Unions 6

1
Defining Structures Defining Structures
• This declaration does not declare any
• A structure is a data type which defines variables; it specifies a new type called Book
a particular kind of object of our choosing • The compiler will use this definition as a
struct Book
blueprint for creating entities of type Book
{ • The elements title, author, publisher and
The keyword struct year enclosed between braces are usually
char title [80];
declares that Book referred to as data members of the structure
char author [80]; Book
is structure
char publisher [80];
• Every variable of type Book will contain the
int year; members title, author, publisher and year
};
Programming Fundamentals– Structures & Unions 7 Programming Fundamentals– Structures & Unions 8

Defining Structures Declaring a Structure Object


• We can define a variable of type Book in
• The data members of a structure can exactly the same way that we use in
be of any type, except the type of the defining variables of any other type
structure being defined therefore, • For example;
Book oopBook;
• The structure definition of Book must Book *travelGuide;
not contain a data member of type Book languageGuide[10];
Book • It is possible to define variable when we
define structure type

Programming Fundamentals– Structures & Unions 9 Programming Fundamentals– Structures & Unions 10

Declaring a Structure Object Initializing a Structure Variable


struct Book • We can define initial values for the data
{
members in the definition
char title [80]; This statement defines • Suppose we want to initialize the variable
the type, Book, and
char author [80]; then defines two novel with the data for a specific book
char publisher [80]; variables of that type
Book oopBook =
namely novel and
int year; dictionary {
}novel, dictionary; “Beginning C++”,
“Ivor Horton’s”,
It’s good to write a separate statement for the “Wrox”,
type definition, and for each of the object 1998
declarations };
Programming Fundamentals– Structures & Unions 11 Programming Fundamentals– Structures & Unions 12

2
Initializing an Array of Structure Accessing the Members of a
Variables Structure Variable
• To access individual data members of a structure
Book oopBook []= variable, we can use the member access operator (.),
{
{“Beginning C++”,“Ivor Horton’s”,“Wrox”,1998},
• To refer to a particular data member, we write the
{“OOP in C++”,“ Lafore”,“SAMS”,1998}, structure variable name, followed by a period (.) or
{“Programming in C++”,“John”,“McGrawHill”,1996} dot operator
}; – oopBook.year = 1988;

• Variable members of a structure can be used in


This statement creates and initializes the array, arithmetic expressions exactly the same way as we
oopBook, of type Book, and the array will have use any other variable of same type
three elements
– oopBook.year + 2;
Programming Fundamentals– Structures & Unions 13 Programming Fundamentals– Structures & Unions 14

Member Functions of Structure


Member Functions of Structure
• Many programmers refer that a structure is used
merely as a collection of data items • We can define the volume() within the Box
• A C++ structure is a class, therefore, it is also type definition, to make it a part of objects of
possible for it to support functionality that type Box
• In our previous Box example, the volume() has • The volume() function will be a member of
relevance only with the Box object and it has no
the type Box
purpose in any other context
• It is preferable to integrate the function into the
Box type

Programming Fundamentals– Structures & Unions 15 Programming Fundamentals– Structures & Unions 16

Member Functions of Structure Member Functions of Structure


struct Box
• This definition has two consequences
{
• First, when we call the function, we don’t need to
int length;
int breadth; include arguments
int height; • Second, the data members in the return
//Function to calculate the volume expression don’t need an object name to qualify
of a Box (access) them
double volume(){
• To call this function for an object, we just use the
return length * breadth * height;
member selection operator (.) in the same way as
}
}; we did for data members
Programming Fundamentals– Structures & Unions 17 Programming Fundamentals– Structures & Unions 18

3
Placing the Member Function
Member Functions of Structure Definition
• We can put the declaration for the member
• Calling member Function example function within the structure definition and
define the member function separately
Box firstBox = {80.0, 50.0, 40.0};
double vol = firstBox.volume(); struct Box
{
• This volume() function can only be applied to
int length;
Box objects
int breadth;
• If we don’t have Box object then we cannot
int height;
use the volume() function
double volume();//Function Declaration
};
Programming Fundamentals– Structures & Unions 19 Programming Fundamentals– Structures & Unions 20

Placing the Member Function Using Pointers with a Structure


Definition • We can create a pointer to variable of a structure
type
• In this case the function definition will be • For example,
separate from the definition of the structure Box *pBox = 0;
• In the function definition, we must tell the • Assume we have already defined a Box object
compiler that the function being defined is a called aBox, we can set our pointer to the address
member of structure Box of this variable
• For this we use scope resolution operator (::) pBox = &aBox;
• We can use the pointer pBox to access the data
double Box::volume() members of the object to which it points
{ (*pBox).height += 10.0;
return length * breadth * height; • Parentheses are essential because (.) takes
} precedence over (*)
Programming Fundamentals– Structures & Unions 21 Programming Fundamentals– Structures & Unions 22

Pointer Member Access Operator


Pointer Member Access Operator
• If parentheses are missing the compiler would
interpret it as follows
• Pointer member access operator enables us
*(pBox.height) += 10.0;
to express the same thing in a readable and
• This would attempt to treat the pointer pBox as a clear form
struct, and to dereference the expression
• Instead of the previous ambiguous statement
pBox.height, so it will not compile
*(pBox.height) += 10.0;
• This combination of two operators looks ambiguous
and requires explicit use of parentheses, it also does
• We can use the pointer member access
operator
not immediately reflect what we are trying to do
pBox -> height += 10.0;
• For these reasons, we have a special operator, called
the pointer member access operator
Programming Fundamentals– Structures & Unions 23 Programming Fundamentals– Structures & Unions 24

4
Structures with Structures as
Members
• So for, we have used only basic types of
members of structures Enumerated Data Types
• Data members of structures can be of other
types including other structures

Programming Fundamentals– Structures & Unions 25

Enumerated Data Types


Enumerated Data Types
• We sometimes face the need for variables that
• A variable that can assume values
have limited set of possible values for example, corresponding to days of the week, we can
• Number of days in a week define this as follows
• Number of months in a year
• enumeration helps us to handle this problem enum weekDay { Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday };
• We can define an enumeration with a set of
possible values
• This declares an enumerated data type called
• When we create an enumeration, we basically weekDay, and variables of this type can only
create a new type, so it is called enumerated have values from the set that appears between
data type the braces
Programming Fundamentals– Structures & Unions 27 Programming Fundamentals– Structures & Unions 28

Enumerated Data Types Enumerated Data Types

• Each of the names of the days will be


• We could assign explicit values to all the
enumerators, for example
automatically defined as representing a fixed enum Punctuation { Comma = ‘,’, Exclamation = ‘!’,
integer value Question = ‘?’ };
• The first name in the list, Monday, will have the • The values we specify for enumerations must be
compile time constants – the constant
value 0, Tuesday will be 1, and so on through expressions that the compiler can evaluate
Sunday with value 6 • These expressions include literals, enumerations,
• enumerations do not need to have unique values, and const variables, that have been previously
defined
we could define Mon and Monday as the value for
• You can’t use non-const variables even if you
first day have initialized them
Programming Fundamentals– Structures & Unions 29 Programming Fundamentals– Structures & Unions 30

5
Anonymous(Unspecified)
Anonymous(Unspecified) Enumerations
Enumerations • Here we have declared three variables that
• By declaring the variables at the same time can assume values from Monday to Sunday.
as we define the enumeration, we can omit • Since the enumeration type is not specified we
the enumeration type provided that we don’t cannot refer to it
need to declare other variables of this type • We cannot declare other variables for this
later on enumeration at all
enum • A common use of anonymous enumeration
{Monday,Tuesday,Wednesday, type is an alternative way of defining integer
Thursday,Friday,Saturday} constants
Yesterday,Today,Tomorrow; enum{feetPerYard=3,inchesPerFoot=12,
yardsPerMile=1760};
Programming Fundamentals– Structures & Unions 31 Programming Fundamentals– Structures & Unions 32

Synonyms for Data Types

• The typedef keyword enables us to


specify our own data type name as an
alternative type
• typedef simply creates a synonym for a
data type that already exists

Programming Fundamentals– Structures & Unions 33

You might also like