Lecture No 10
Lecture No 10
• 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
• 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
Programming Fundamentals– Structures & Unions 9 Programming Fundamentals– Structures & Unions 10
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;
Programming Fundamentals– Structures & Unions 15 Programming Fundamentals– Structures & Unions 16
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
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
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