SlideShare a Scribd company logo
For more Https://www.ThesisScientist.com
UNIT 6
CLASSES, STRUCTURE,UNION
A class definition consists of two parts: header and body. The class
header specifies the class name and its base classes. (The latter relates to
derived classes and is discussed in Chapter 8.) The class body defines the class
members. Two types of members are supported:
 Data members have the syntax of variable definitions and specify the
representation of class objects.
 Member functions have the syntax of function prototypes and specify the
class operations, also called the class interface.
Class members fall under one of three different access permission
categories:
 Public members are accessible by all class users.
 Private members are only accessible by the class members.
 Protected members are only accessible by the class members and the
members of a derived class.
The data type defined by a class is used in exactly the same way as a built-
in type.
A Simple Class
Listing 6.1 shows the definition of a simple class for representing points in two
dimensions.
Listing 6.1
1
2
3
4
5
6
class Point {
int xVal, yVal;
public:
void SetPt (int, int);
void OffsetPt (int, int);
};
1 This line contains the class header and names the class as Point. A class
definition always begins with the keyword class, followed by the class
name. An open brace marks the beginning of the class body.
2 This line defines two data members, xVal and yVal, both of type int.
The default access permission for a class member is private. Both xVal
and yVal are therefore private.
3 This keyword specifies that from this point onward the class members are
public.
4-5 These two are public member functions. Both have two integer
parameters and a void return type.
6 This brace marks the end of the class body.
The order in which the data and member functions of a class are presented
is largely irrelevant. The above class, for example, may be equivalently written
as:
class Point {
public:
void SetPt (int, int);
void OffsetPt (int, int);
private:
int xVal, yVal;
};
Inline Member Funtion
Just as global functions may be defined to be inline, so can the member
functions of a class. In the class Point, for example, both member functions
are very short (only two statements). Defining these to be inline improves the
efficiency considerably. A member function is defined to be inline by inserting
the keyword inline before its definition.
inline void Point::SetPt (int x,int y)
{
xVal = x;
yVal = y;
}
An easier way of defining member functions to be inline is to include their
definition inside the class.
class Point {
int xVal, yVal;
public:
void SetPt (int x,int y) { xVal = x; yVal = y; }
void OffsetPt (int x,int y) { xVal += x; yVal += y; }
};
For more Https://www.ThesisScientist.com
Constructors
It is possible to define and at the same time initialize objects of a class. This is
supported by special member functions called constructors. A constructor
always has the same name as the class itself. It never has an explicit return
type. For example,
class Point {
int xVal, yVal;
public:
Point (int x,int y) {xVal = x; yVal = y;} // constructor
void OffsetPt (int,int);
};
is an alternative definition of the Point class, where SetPt has been replaced
by a constructor, which in turn is defined to be inline.
Now we can define objects of type Point and initialize them at once. This
is in fact compulsory for classes that contain constructors that require
arguments:
Point pt1 = Point(10,20);
Point pt2; // illegal!
The former can also be specified in an abbreviated form.
Point pt1(10,20);
A class may have more than one constructor. To avoid ambiguity,
however, each of these must have a unique signature. For example,
class Point {
int xVal, yVal;
public:
Point (int x, int y) { xVal = x; yVal = y; }
Point (float, float); // polar coordinates
Point (void) { xVal = yVal = 0; } // origin
void OffsetPt (int, int);
};
Point::Point (float len, float angle) // polar coordinates
{
xVal = (int) (len * cos(angle));
yVal = (int) (len * sin(angle));
}
offers three different constructors. An object of type Point can be defined
using any of these:
Point pt1(10,20); // cartesian coordinates
Point pt2(60.3,3.14); // polar coordinates
Point pt3; // origin
The Set class can be improved by using a constructor instead of
EmptySet:
class Set {
public:
Set (void) { card = 0; }
//...
};
This has the distinct advantage that the programmer need no longer remember
to call EmptySet. The constructor ensures that every set is initially empty.
The Set class can be further improved by giving the user control over the
maximum size of a set. To do this, we define elems as an integer pointer
rather than an integer array. The constructor can then be given an argument
which specifies the desired size. This means that maxCard will no longer be the
same for all Set objects and therfore needs to become a data member itself:
class Set {
public:
Set (const int size);
//...
private:
int *elems; // set elements
int maxCard; // maximum cardinality
int card; // set cardinality
};
The constructor simply allocates a dynamic array of the desired size and
initializes maxCard and card accordingly:
Set::Set (const int size)
{
elems = new int[size];
maxCard = size;
card = 0;
}
It is now possible to define sets of different maximum sizes:
Set ages(10), heights(20), primes(100);
It is important to note that an object’s constructor is applied when the
object is created. This in turn depends on the object’s scope. For example, a
global object is created as soon as program execution commences; an
automatic object is created when its scope is entered; and a dynamic object is
created when the new operator is applied to it.
For more Https://www.ThesisScientist.com
Destructors
Just as a constructor is used to initialize an object when it is created, a
destructor is used to clean up the object just before it is destroyed. A
destructor always has the same name as the class itself, but is preceded with a
~ symbol. Unlike constructors, a class may have at most one destructor. A
destructor never takes any arguments and has no explicit return type.
Destructors are generally useful for classes which have pointer data
members which point to memory blocks allocated by the class itself. In such
cases it is important to release member-allocated memory before the object is
destroyed. A destructor can do just that.
For example, our revised version of Set uses a dynamically-allocated
array for the elems member. This memory should be released by a destructor:
class Set {
public:
Set (const int size);
~Set (void) {delete elems;} // destructor
//...
private:
int *elems; // set elements
int maxCard; // maximum cardinality
int card; // set cardinality
};
Now consider what happens when a Set is defined and used in a function:
void Foo (void)
{
Set s(10);
//...
}
When Foo is called, the constructor for s is invoked, allocating storage for
s.elems and initializing its data members. Next the rest of the body of Foo is
executed. Finally, before Foo returns, the destructor for s is invoked, deleting
the storage occupied by s.elems. Hence, as far as storage allocation is
concerned, s behaves just like an automatic variable of a built-in type, which is
created when its scope is entered and destroyed when its scope is left.
In general, an object’s constructor is applied just before the object is
destroyed. This in turn depends on the object’s scope. For example, a global
object is destroyed when program execution is completed; an automatic object
is destroyed when its scope is left; and a dynamic object is destroyed when the
delete operator is applied to it.
Friends
Occasionally we may need to grant a function access to the nonpublic
members of a class. Such an access is obtained by declaring the function a
friend of the class. There are two possible reasons for requiring this access:
 It may be the only correct way of defining the function.
 It may be necessary if the function is to be implemented efficiently.
Examples of the first case will be provided in Chapter 7, when we discuss
overloaded input/output operators. An example of the second case is
discussed below.
Suppose that we have defined two variants of the Set class, one for sets
of integers and one for sets of reals:
class IntSet {
public:
//...
private:
int elems[maxCard];
int card;
};
class RealSet {
public:
//...
private:
float elems[maxCard];
int card;
};
We want to define a function, SetToReal, which converts an integer set to a
real set. We can do this by making the function a member of IntSet:
void IntSet::SetToReal (RealSet &set)
{
set.EmptySet();
for (register i = 0; i < card; ++i)
set.AddElem((float) elems[i]);
}
Although this works, the overhead of calling AddElem for every member of the
set may be unacceptable. The implementation can be improved if we could
gain access to the private members of both IntSet and RealSet. This can be
arranged by declaring SetToReal as a friend of RealSet.
class RealSet {
//...
friend void IntSet::SetToReal (RealSet&);
};
void IntSet::SetToReal (RealSet &set)
{
For more Https://www.ThesisScientist.com
set.card = card;
for (register i = 0; i < card; ++i)
set.elems[i] = (float) elems[i];
}
The extreme case of having all member functions of a class A as friends of
another class B can be expressed in an abbreviated form:
class A;
class B {
//...
friend class A; // abbreviated form
};
Although a friend declaration appears inside a class, that does not make
the function a member of that class. In general, the position of a friend
declaration in a class is irrelevant: whether it appears in the private, protected,
or the public section, it has the same meaning.
Default Arguments
As with global functions, a member function of a class may have default
arguments. The same rules apply: all default arguments should be trailing
arguments, and the argument should be an expression consisting of objects
defined within the scope in which the class appears.
For example, a constructor for the Point class may use default arguments
to provide more variations of the way a Point object may be defined:
class Point {
int xVal, yVal;
public:
Point (int x = 0, int y = 0);
//...
};
Given this constructor, the following definitions are all valid:
Point p1; // same as: p1(0, 0)
Point p2(10); // same as: p2(10, 0)
Point p3(10, 20);
Careless use of default arguments can lead to undesirable ambiguity. For
example, given the class
class Point {
int xVal, yVal;
public:
Point (int x = 0, int y = 0);
Point (float x = 0, float y = 0); // polar coordinates
//...
};
For more Https://www.ThesisScientist.com
Implicit Member Argument
When a class member function is called, it receives an implicit argument which
denotes the particular object (of the class) for which the function is invoked.
For example, in
Point pt(10,20);
pt.OffsetPt(2,2);
pt is an implicit argument to OffsetPt. Within the body of the member function, one can
refer to this implicit argument explicitly as this, which denotes a pointer to the object for
which the member is invoked. Using this, OffsetPt can be rewritten as:
Point::OffsetPt (int x, int y)
{
this->xVal += x; // equivalent to: xVal += x;
this->yVal += y; // equivalent to: yVal += y;
}
Use of this in this particular example is redundant. There are, however,
programming cases where the use of the this pointer is essential. We will see
examples of such cases in Chapter 7, when discussing overloaded operators.
The this pointer can be used for referring to member functions in exactly
the same way as it is used for data members. It is important to bear in mind,
however, that this is defined for use within member functions of a class only.
In particular, it is undefined for global functions (including global friend
functions).
Scope Operator
When calling a member function, we usually use an abbreviated syntax. For
example:
pt.OffsetPt(2,2); // abbreviated form
This is equivalent to the full form:
pt.Point::OffsetPt(2,2); // full form
The full form uses the binary scope operator :: to indicate that OffsetPt is a
member of Point.
In some situations, using the scope operator is essential. For example, the
case where the name of a class member is hidden by a local variable (e.g.,
member function parameter) can be overcome using the scope operator:
class Point {
public:
Point (int x, int y) { Point::x = x; Point::y = y; }
//...
private:
int x, y;
}
Here x and y in the constructor (inner scope) hide x and y in the class (outer
scope). The latter are referred to explicitly as Point::x and Point::y.
A member initialization list
There are two ways of initializing the data members of a class. The first
approach involves initializing the data members using assignments in the body
of a constructor. For example:
class Image {
public:
Image (const int w, const int h);
private:
int width;
int height;
//...
};
Image::Image (const int w, const int h)
{
width = w;
height = h;
//...
}
The second approach uses a member initialization list in the definition
of a constructor. For example:
class Image {
public:
Image (const int w, const int h);
private:
int width;
int height;
//...
};
Image::Image (const int w, const int h) : width(w), height(h)
{
//...
}
The effect of this declaration is that width is initialized to w and height is
initialized to h. The only difference between this approach and the previous
one is that here members are initialized before the body of the constructor is
executed.
For more Https://www.ThesisScientist.com
A member initialization list may be used for initializing any data member
of a class. It is always placed between the constructor header and body. A
colon is used to separate it from the header. It should consist of a comma-
separated list of data members whose initial value appears within a pair of
brackets.
Constant Members
A class data member may defined as constant. For example:
class Image {
const int width;
const int height;
//...
};
However, data member constants cannot be initialized using the same syntax
as for other constants:
class Image {
const int width = 256; // illegal initializer!
const int height = 168; // illegal initializer!
//...
};
The correct way to initialize a data member constant is through a member
initialization list:
class Image {
public:
Image (const int w, const int h);
private:
const int width;
const int height;
//...
};
Image::Image (const int w, const int h) : width(w), height(h)
{
//...
}
As one would expect, no member function is allowed to assign to a constant
data member.
A constant data member is not appropriate for defining the dimension of
an array data member. For example, in
class Set {
public:
Set (void) : maxCard(10) { card = 0; }
//...
private:
const maxCard;
int elems[maxCard]; // illegal!
int card;
};
the array elems will be rejected by the compiler for not having a constant
dimension. The reason for this being that maxCard is not bound to a value
during compilation, but when the program is run and the constructor is
invoked.
Member functions may also be defined as constant. This is used to specify
which member functions of a class may be invoked for a constant object. For
example,
class Set {
public:
Set (void) { card = 0; }
Bool Member (const int) const;
void AddElem (const int);
//...
};
Bool Set::Member (const int elem) const
{
//...
}
defines Member as a constant member function. To do so, the keyword const
is inserted after the function header, both inside the class and in the function
definition.
A constant object can only be modified by the constant member functions
of the class:
const Set s;
s.AddElem(10); // illegal: AddElem not a const member
s.Member(10); // ok
The Static Members
A data member of a class can be defined to be static. This ensures that there
will be exactly one copy of the member, shared by all objects of the class. For
example, consider a Window class which represents windows on a bitmap
display:
class Window {
static Window *first; // linked-list of all windows
Window *next; // pointer to next window
//...
};
For more Https://www.ThesisScientist.com
Here, no matter how many objects of type Window are defined, there will be
only one instance of first. Like other static variables, a static data member is
by default initialized to 0. It can be initialized to an arbitrary value in the same
scope where the member function definitions appear:
Window *Window::first = &myWindow;
The alternative is to make such variables global, but this is exactly what static
members are intended to avoid; by including the variable in a class, we can
ensure that it will be inaccessible to anything outside the class.
Member functions can also be defined to be static. Semantically, a static
member function is like a global function which is a friend of the class, but
inaccessible outside the class. It does not receive an implicit argument and
hence cannot refer to this. Static member functions are useful for defining
call-back routines whose parameter lists are predetermined and outside the
control of the programmer.
For example, the Window class might use a call-back function for
repainting exposed areas of the window:
class Window {
//...
static void PaintProc (Event *event); // call-back
};
Referance Members
A class data member may defined as reference. For example:
class Image {
int width;
int height;
int &widthRef;
//...
};
As with data member constants, a data member reference cannot be initialized
using the same syntax as for other references:
class Image {
int width;
int height;
int &widthRef = width; // illegal!
//...
};
The correct way to initialize a data member reference is through a member
initialization list:
class Image {
public:
Image (const int w, const int h);
private:
int width;
int height;
int &widthRef;
//...
};
For more Https://www.ThesisScientist.com
Class Object Members
A data member of a class may be of a user-defined type, that is, an object of
another class. For example, a Rectangle class may be defined using two
Point data members which represent the top-left and bottom-right corners of
the rectangle:
class Rectangle {
public:
Rectangle (int left, int top, int right, int bottom);
//...
private:
Point topLeft;
Point botRight;
};
The constructor for Rectangle should also initialize the two object members
of the class. Assuming that Point has a constructor, this is done by including
topLeft and botRight in the member initialization list of the constructor for
Rectangle:
Rectangle::Rectangle (int left, int top, int right, int bottom)
: topLeft(left,top), botRight(right,bottom)
{
}
If the constructor for Point takes no parameters, or if it has default arguments
for all of its parameters, then the above member initialization list may be
omitted. Of course, the constructor is still implicitly called.
The order of initialization is always as follows. First, the constructor for
topLeft is invoked, followed by the constructor for botRight, and finally the
constructor for Rectangle itself. Object destruction always follows the
opposite direction. First the destructor for Rectangle (if any) is invoked,
followed by the destructor for botRight, and finally for topLeft. The reason
that topLeft is initialized before botRight is not that it appears first in the
member initialization list, but because it appears before botRight in the class
itself. Therefore, defining the constructor as follows would not change the
initialization (or destruction) order:
Rectangle::Rectangle (int left, int top, int right, int bottom)
: botRight(right,bottom), topLeft(left,top)
{
}
Object Arrays
An array of a user-defined type is defined and used much in the same way as
an array of a built-in type. For example, a pentagon can be defined as an array
of 5 points:
Point pentagon[5];
This definition assumes that Point has an ‘argument-less’ constructor (i.e.,
one which can be invoked without arguments). The constructor is applied to
each element of the array.
The array can also be initialized using a normal array initializer. Each
entry in the initialization list would invoke the constructor with the desired
arguments. When the initializer has less entries than the array dimension, the
remaining elements are initialized by the argument-less constructor. For
example,
Point pentagon[5] = {
Point(10,20), Point(10,30), Point(20,30), Point(30,20)
};
initializes the first four elements of pentagon to explicit points, and the last
element is initialized to (0,0).
When the constructor can be invoked with a single argument, it is
sufficient to just specify the argument. For example,
Set sets[4] = {10, 20, 20, 30};
is an abbreviated version of:
Set sets[4] = {Set(10), Set(20), Set(20), Set(30)};
An array of objects can also be created dynamically using new:
Point *petagon = new Point[5];
When the array is finally deleted using delete, a pair of [] should be
included:
delete [] pentagon; // destroys all array elements
Unless the [] is included, delete will have no way of knowing that pentagon
denotes an array of points and not just a single point. The destructor (if any) is
applied to the elements of the array in reverse order before the array is deleted.
Omitting the [] will cause the destructor to be applied to just the first element
of the array:
delete pentagon; // destroys only the first element!
For more Https://www.ThesisScientist.com
Since the objects of a dynamic array cannot be explicitly initialized at the
time of creation, the class must have an argument-less constructor to handle
the implicit initialization. When this implicit initialization is insufficient, the
programmer can explicitly reinitialize any of the elements later:
pentagon[0].Point(10, 20);
pentagon[1].Point(10, 30);
//...
Structures and Unions
A structure is a class all of whose members are by default public. (Remember
that all of the members of a class are by default private.) Structures are defined
using the same syntax as classes, except that the keyword struct is used
instead of class. For example,
struct Point {
Point (int, int);
void OffsetPt (int, int);
int x, y;
};
is equivalent to:
class Point {
public:
Point (int, int);
void OffsetPt (int, int);
int x, y;
};
The struct construct originated in C, where it could only contain data
members. It has been retained mainly for backward compatibility reasons. In
C, a structure can have an initializer with a syntax similar to that of an array.
C++ allows such initializers for structures and classes all of whose data
members are public:
class Employee {
public:
char *name;
int age;
double salary;
};
Employee emp = {"Jack", 24, 38952.25};
The initializer consists of values which are assigned to the data members of the
structure (or class) in the order they appear. This style of initialization is
largely superseded by constructors. Furthermore, it cannot be used with a
class that has a constructor.
A union is a class all of whose data members are mapped to the same
address within its object (rather than sequentially as is the case in a class). The
size of an object of a union is, therefore, the size of its largest data member.
The main use of unions is for situations where an object may assume
values of different types, but only one at a time. For example, consider an
interpreter for a simple programming language, called P, which supports a
number of data types such as: integers, reals, strings, and lists. A value in this
language may be defined to be of the type:
For more Https://www.ThesisScientist.com
union Value {
long integer;
double real;
char *string;
Pair list;
//...
};
where Pair is itself a user-defined type for creating lists:
class Pair {
Value *head;
Value *tail;
//...
};
Assuming that a long is 4 bytes, a double 8 bytes, and a pointer 4 bytes, an
object of type Value would be exactly 8 bytes, i.e., the same as the size of a
double or a Pair object (the latter being equal to two pointers).
An object in P can be represented by the class,
class Object {
private:
enum ObjType {intObj, realObj, strObj, listObj};
ObjType type; // object type
Value val; // object value
//...
};
where type provides a way of recording what type of value the object
currently has. For example, when type is set to strObj, val.string is used
for referring to its value.
Because of the unique way in which its data members are mapped to
memory, a union may not have a static data member or a data member which
requires a constructor.
Like a structure, all of the members of a union are by default public. The
keywords private, public, and protected may be used inside a struct or
a union in exactly the same way they are used inside a class for defining
private, public, and protected members.
Bit Fields
It is sometimes desirable to directly control an object at the bit level, so that as
many individual data items as possible can be packed into a bit stream without
worrying about byte or word boundaries.
For example, in data communication, data is transferred in discrete units
called packets. In addition to the user data that it carries, each packet also
contains a header which is comprised of network-related information for
managing the transmission of the packet across the network. To minimize the
cost of transmission, it is desirable to minimize the space taken by the header.
Figure 6.1 illustrates how the header fields are packed into adjacent bits to
achieve this.
Figure 6.1 Header fields of a packet.
type
acknowledge
channel
sequenceNo
moreData
These fields can be expressed as bit field data members of a Packet class.
A bit field may be defined to be of type int or unsigned int:
typedef unsigned int Bit;
class Packet {
Bit type : 2; // 2 bits wide
Bit acknowledge : 1; // 1 bit wide
Bit channel : 4; // 4 bits wide
Bit sequenceNo : 4; // 4 bite wide
Bit moreData : 1; // 1 bit wide
//...
};
A bit field is referred to in exactly the same way as any other data
member. Because a bit field does not necessarily start on a byte boundary, it is
illegal to take its address. For the same reason, a bit field cannot be defined as
static.
Use of enumerations can make working with bit fields easier. For
example, given the enumerations
enum PacketType {dataPack, controlPack, supervisoryPack};
enum Bool {false, true};
we can write:
Packet p;
p.type = controlPack;
p.acknowledge = true;

More Related Content

What's hot (20)

PDF
Rau trái & biến đổi sinh hóa
Food chemistry-09.1800.1595
 
DOC
Cn enzyme
Luong NguyenThanh
 
PDF
Suy diễn thống kê và ngôn ngữ R (4): Phân tích phương sai (ANOVA)
Tài Tài
 
PDF
qua-trinh-thiet-bi-trong-cnhh-thuc-pham-3__hc3_06_2019_trich-ly - [cuuduongth...
MinhMinh312121
 
PDF
GIÁO TRÌNH MÔ ĐUN PHÒNG TRỪ SÂU BỆNH HẠI
Thái Nguyễn Văn
 
PPT
BÀI 4: ĐỘT BIẾN GEN
Hue Nguyen
 
PDF
CHUYỂN HÓA PROTEIN
SoM
 
PDF
Nghiên cứu quá trình tách chiết và tinh sạch protein trong rong bún enteromor...
TÀI LIỆU NGÀNH MAY
 
PPTX
Khảo sát dinh dưỡng cho nhóm sinh viên
LoanL67
 
PPTX
Cauchy integral theorem &amp; formula (complex variable & numerical method )
Digvijaysinh Gohil
 
PPT
Tiet 12 sản xuat amylaza và đường hóa tinh bột
Chu Kien
 
PPT
Bao quan thuc pham
Đỗ Công Thịnh
 
PDF
Finite Difference Method
Syeilendra Pramuditya
 
PPTX
Newton Raphson Method
Barkha Gupta
 
PDF
03-database-management-system-revision-notes.pdf
Amit Mishra
 
PDF
Tổng hợp các câu trắc nghiệm vi sinh đại cương
Huy Hoang
 
PDF
Khả năng loại màu thuốc nhuộm của VSV sinh enzyme laccase, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
PDF
Giáo trình công nghệ sinh học động vật
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
PPT
Trao doi chat va q p1
Pham Ngoc Quang
 
PPTX
Thu y. c4. bệnh phó thương hàn lợn
SinhKy-HaNam
 
Rau trái & biến đổi sinh hóa
Food chemistry-09.1800.1595
 
Suy diễn thống kê và ngôn ngữ R (4): Phân tích phương sai (ANOVA)
Tài Tài
 
qua-trinh-thiet-bi-trong-cnhh-thuc-pham-3__hc3_06_2019_trich-ly - [cuuduongth...
MinhMinh312121
 
GIÁO TRÌNH MÔ ĐUN PHÒNG TRỪ SÂU BỆNH HẠI
Thái Nguyễn Văn
 
BÀI 4: ĐỘT BIẾN GEN
Hue Nguyen
 
CHUYỂN HÓA PROTEIN
SoM
 
Nghiên cứu quá trình tách chiết và tinh sạch protein trong rong bún enteromor...
TÀI LIỆU NGÀNH MAY
 
Khảo sát dinh dưỡng cho nhóm sinh viên
LoanL67
 
Cauchy integral theorem &amp; formula (complex variable & numerical method )
Digvijaysinh Gohil
 
Tiet 12 sản xuat amylaza và đường hóa tinh bột
Chu Kien
 
Bao quan thuc pham
Đỗ Công Thịnh
 
Finite Difference Method
Syeilendra Pramuditya
 
Newton Raphson Method
Barkha Gupta
 
03-database-management-system-revision-notes.pdf
Amit Mishra
 
Tổng hợp các câu trắc nghiệm vi sinh đại cương
Huy Hoang
 
Khả năng loại màu thuốc nhuộm của VSV sinh enzyme laccase, HAY
Dịch vụ viết bài trọn gói ZALO 0917193864
 
Giáo trình công nghệ sinh học động vật
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Trao doi chat va q p1
Pham Ngoc Quang
 
Thu y. c4. bệnh phó thương hàn lợn
SinhKy-HaNam
 

Similar to CLASSES, STRUCTURE,UNION in C++ (20)

PDF
Op ps
Shehzad Rizwan
 
PPT
Bca 2nd sem u-2 classes & objects
Rai University
 
PPT
Mca 2nd sem u-2 classes & objects
Rai University
 
PPTX
Class and object
prabhat kumar
 
PPT
Object Oriented Programming Examples with explanation
ulhaq18
 
PPTX
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
PDF
Classes and object
Ankit Dubey
 
PDF
Chapter 12 PJPK SDSDRFHVRCHVFHHVDRHVDRVHGVD
azimah6642
 
PPT
Unit vi(dsc++)
Durga Devi
 
PDF
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
mradeen946
 
PPTX
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Abu Saleh
 
PPT
oop objects_classes
sidra tauseef
 
PPTX
Classes and objects
Anil Kumar
 
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
PPT
data Structure Lecture 1
Teksify
 
PPTX
Class and object
MushfiqurRahaman7
 
PDF
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
PPTX
CSCI 238 Chapter 07 - Classes
DanWooster1
 
PPTX
OOPs & C++ UNIT 3
Dr. SURBHI SAROHA
 
PPTX
Chapter 13 introduction to classes
rsnyder3601
 
Bca 2nd sem u-2 classes & objects
Rai University
 
Mca 2nd sem u-2 classes & objects
Rai University
 
Class and object
prabhat kumar
 
Object Oriented Programming Examples with explanation
ulhaq18
 
Lecture 4.2 c++(comlete reference book)
Abu Saleh
 
Classes and object
Ankit Dubey
 
Chapter 12 PJPK SDSDRFHVRCHVFHHVDRHVDRVHGVD
azimah6642
 
Unit vi(dsc++)
Durga Devi
 
Unit_2_oop By Alfiya Sayyed Maam from AIARKP
mradeen946
 
Lecture 2, c++(complete reference,herbet sheidt)chapter-12
Abu Saleh
 
oop objects_classes
sidra tauseef
 
Classes and objects
Anil Kumar
 
22 scheme OOPs with C++ BCS306B_module1.pdf
sindhus795217
 
data Structure Lecture 1
Teksify
 
Class and object
MushfiqurRahaman7
 
classes and objects.pdfggggggggffffffffgggf
gurpreetk8199
 
CSCI 238 Chapter 07 - Classes
DanWooster1
 
OOPs & C++ UNIT 3
Dr. SURBHI SAROHA
 
Chapter 13 introduction to classes
rsnyder3601
 
Ad

More from Prof Ansari (20)

PDF
Sci Hub New Domain
Prof Ansari
 
PDF
Sci Hub cc Not Working
Prof Ansari
 
PDF
basics of computer network
Prof Ansari
 
PDF
JAVA INTRODUCTION
Prof Ansari
 
PDF
Project Evaluation and Estimation in Software Development
Prof Ansari
 
PDF
Stepwise Project planning in software development
Prof Ansari
 
PDF
Database and Math Relations
Prof Ansari
 
PDF
Normalisation in Database management System (DBMS)
Prof Ansari
 
PDF
Entity-Relationship Data Model in DBMS
Prof Ansari
 
PDF
A Detail Database Architecture
Prof Ansari
 
PDF
INTRODUCTION TO Database Management System (DBMS)
Prof Ansari
 
PDF
Master thesis on Vehicular Ad hoc Networks (VANET)
Prof Ansari
 
PDF
Master Thesis on Vehicular Ad-hoc Network (VANET)
Prof Ansari
 
PDF
INTERFACING WITH INTEL 8251A (USART)
Prof Ansari
 
PDF
HOST AND NETWORK SECURITY by ThesisScientist.com
Prof Ansari
 
PDF
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
Prof Ansari
 
PDF
INTRODUCTION TO VISUAL BASICS
Prof Ansari
 
PDF
introduction to Blogging ppt
Prof Ansari
 
PDF
INTRODUCTION TO SOFTWARE ENGINEERING
Prof Ansari
 
PDF
Introduction to E-commerce
Prof Ansari
 
Sci Hub New Domain
Prof Ansari
 
Sci Hub cc Not Working
Prof Ansari
 
basics of computer network
Prof Ansari
 
JAVA INTRODUCTION
Prof Ansari
 
Project Evaluation and Estimation in Software Development
Prof Ansari
 
Stepwise Project planning in software development
Prof Ansari
 
Database and Math Relations
Prof Ansari
 
Normalisation in Database management System (DBMS)
Prof Ansari
 
Entity-Relationship Data Model in DBMS
Prof Ansari
 
A Detail Database Architecture
Prof Ansari
 
INTRODUCTION TO Database Management System (DBMS)
Prof Ansari
 
Master thesis on Vehicular Ad hoc Networks (VANET)
Prof Ansari
 
Master Thesis on Vehicular Ad-hoc Network (VANET)
Prof Ansari
 
INTERFACING WITH INTEL 8251A (USART)
Prof Ansari
 
HOST AND NETWORK SECURITY by ThesisScientist.com
Prof Ansari
 
SYSTEM NETWORK ADMINISTRATIONS GOALS and TIPS
Prof Ansari
 
INTRODUCTION TO VISUAL BASICS
Prof Ansari
 
introduction to Blogging ppt
Prof Ansari
 
INTRODUCTION TO SOFTWARE ENGINEERING
Prof Ansari
 
Introduction to E-commerce
Prof Ansari
 
Ad

Recently uploaded (20)

PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Snet+Pro+Service+Software_SNET+Pro+2+Instructions.pptx
jenilsatikuvar1
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Thermal runway and thermal stability.pptx
godow93766
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 

CLASSES, STRUCTURE,UNION in C++

  • 1. For more Https://www.ThesisScientist.com UNIT 6 CLASSES, STRUCTURE,UNION A class definition consists of two parts: header and body. The class header specifies the class name and its base classes. (The latter relates to derived classes and is discussed in Chapter 8.) The class body defines the class members. Two types of members are supported:  Data members have the syntax of variable definitions and specify the representation of class objects.  Member functions have the syntax of function prototypes and specify the class operations, also called the class interface. Class members fall under one of three different access permission categories:  Public members are accessible by all class users.  Private members are only accessible by the class members.  Protected members are only accessible by the class members and the members of a derived class. The data type defined by a class is used in exactly the same way as a built- in type. A Simple Class Listing 6.1 shows the definition of a simple class for representing points in two dimensions. Listing 6.1 1 2 3 4 5 6 class Point { int xVal, yVal; public: void SetPt (int, int); void OffsetPt (int, int); }; 1 This line contains the class header and names the class as Point. A class definition always begins with the keyword class, followed by the class name. An open brace marks the beginning of the class body. 2 This line defines two data members, xVal and yVal, both of type int. The default access permission for a class member is private. Both xVal and yVal are therefore private.
  • 2. 3 This keyword specifies that from this point onward the class members are public. 4-5 These two are public member functions. Both have two integer parameters and a void return type. 6 This brace marks the end of the class body. The order in which the data and member functions of a class are presented is largely irrelevant. The above class, for example, may be equivalently written as: class Point { public: void SetPt (int, int); void OffsetPt (int, int); private: int xVal, yVal; }; Inline Member Funtion Just as global functions may be defined to be inline, so can the member functions of a class. In the class Point, for example, both member functions are very short (only two statements). Defining these to be inline improves the efficiency considerably. A member function is defined to be inline by inserting the keyword inline before its definition. inline void Point::SetPt (int x,int y) { xVal = x; yVal = y; } An easier way of defining member functions to be inline is to include their definition inside the class. class Point { int xVal, yVal; public: void SetPt (int x,int y) { xVal = x; yVal = y; } void OffsetPt (int x,int y) { xVal += x; yVal += y; } };
  • 3. For more Https://www.ThesisScientist.com Constructors It is possible to define and at the same time initialize objects of a class. This is supported by special member functions called constructors. A constructor always has the same name as the class itself. It never has an explicit return type. For example, class Point { int xVal, yVal; public: Point (int x,int y) {xVal = x; yVal = y;} // constructor void OffsetPt (int,int); }; is an alternative definition of the Point class, where SetPt has been replaced by a constructor, which in turn is defined to be inline. Now we can define objects of type Point and initialize them at once. This is in fact compulsory for classes that contain constructors that require arguments: Point pt1 = Point(10,20); Point pt2; // illegal! The former can also be specified in an abbreviated form. Point pt1(10,20); A class may have more than one constructor. To avoid ambiguity, however, each of these must have a unique signature. For example, class Point { int xVal, yVal; public: Point (int x, int y) { xVal = x; yVal = y; } Point (float, float); // polar coordinates Point (void) { xVal = yVal = 0; } // origin void OffsetPt (int, int); }; Point::Point (float len, float angle) // polar coordinates { xVal = (int) (len * cos(angle)); yVal = (int) (len * sin(angle)); } offers three different constructors. An object of type Point can be defined using any of these: Point pt1(10,20); // cartesian coordinates Point pt2(60.3,3.14); // polar coordinates
  • 4. Point pt3; // origin The Set class can be improved by using a constructor instead of EmptySet: class Set { public: Set (void) { card = 0; } //... }; This has the distinct advantage that the programmer need no longer remember to call EmptySet. The constructor ensures that every set is initially empty. The Set class can be further improved by giving the user control over the maximum size of a set. To do this, we define elems as an integer pointer rather than an integer array. The constructor can then be given an argument which specifies the desired size. This means that maxCard will no longer be the same for all Set objects and therfore needs to become a data member itself: class Set { public: Set (const int size); //... private: int *elems; // set elements int maxCard; // maximum cardinality int card; // set cardinality }; The constructor simply allocates a dynamic array of the desired size and initializes maxCard and card accordingly: Set::Set (const int size) { elems = new int[size]; maxCard = size; card = 0; } It is now possible to define sets of different maximum sizes: Set ages(10), heights(20), primes(100); It is important to note that an object’s constructor is applied when the object is created. This in turn depends on the object’s scope. For example, a global object is created as soon as program execution commences; an automatic object is created when its scope is entered; and a dynamic object is created when the new operator is applied to it.
  • 5. For more Https://www.ThesisScientist.com Destructors Just as a constructor is used to initialize an object when it is created, a destructor is used to clean up the object just before it is destroyed. A destructor always has the same name as the class itself, but is preceded with a ~ symbol. Unlike constructors, a class may have at most one destructor. A destructor never takes any arguments and has no explicit return type. Destructors are generally useful for classes which have pointer data members which point to memory blocks allocated by the class itself. In such cases it is important to release member-allocated memory before the object is destroyed. A destructor can do just that. For example, our revised version of Set uses a dynamically-allocated array for the elems member. This memory should be released by a destructor: class Set { public: Set (const int size); ~Set (void) {delete elems;} // destructor //... private: int *elems; // set elements int maxCard; // maximum cardinality int card; // set cardinality }; Now consider what happens when a Set is defined and used in a function: void Foo (void) { Set s(10); //... } When Foo is called, the constructor for s is invoked, allocating storage for s.elems and initializing its data members. Next the rest of the body of Foo is executed. Finally, before Foo returns, the destructor for s is invoked, deleting the storage occupied by s.elems. Hence, as far as storage allocation is concerned, s behaves just like an automatic variable of a built-in type, which is created when its scope is entered and destroyed when its scope is left. In general, an object’s constructor is applied just before the object is destroyed. This in turn depends on the object’s scope. For example, a global object is destroyed when program execution is completed; an automatic object is destroyed when its scope is left; and a dynamic object is destroyed when the delete operator is applied to it.
  • 6. Friends Occasionally we may need to grant a function access to the nonpublic members of a class. Such an access is obtained by declaring the function a friend of the class. There are two possible reasons for requiring this access:  It may be the only correct way of defining the function.  It may be necessary if the function is to be implemented efficiently. Examples of the first case will be provided in Chapter 7, when we discuss overloaded input/output operators. An example of the second case is discussed below. Suppose that we have defined two variants of the Set class, one for sets of integers and one for sets of reals: class IntSet { public: //... private: int elems[maxCard]; int card; }; class RealSet { public: //... private: float elems[maxCard]; int card; }; We want to define a function, SetToReal, which converts an integer set to a real set. We can do this by making the function a member of IntSet: void IntSet::SetToReal (RealSet &set) { set.EmptySet(); for (register i = 0; i < card; ++i) set.AddElem((float) elems[i]); } Although this works, the overhead of calling AddElem for every member of the set may be unacceptable. The implementation can be improved if we could gain access to the private members of both IntSet and RealSet. This can be arranged by declaring SetToReal as a friend of RealSet. class RealSet { //... friend void IntSet::SetToReal (RealSet&); }; void IntSet::SetToReal (RealSet &set) {
  • 7. For more Https://www.ThesisScientist.com set.card = card; for (register i = 0; i < card; ++i) set.elems[i] = (float) elems[i]; } The extreme case of having all member functions of a class A as friends of another class B can be expressed in an abbreviated form: class A; class B { //... friend class A; // abbreviated form }; Although a friend declaration appears inside a class, that does not make the function a member of that class. In general, the position of a friend declaration in a class is irrelevant: whether it appears in the private, protected, or the public section, it has the same meaning. Default Arguments As with global functions, a member function of a class may have default arguments. The same rules apply: all default arguments should be trailing arguments, and the argument should be an expression consisting of objects defined within the scope in which the class appears. For example, a constructor for the Point class may use default arguments to provide more variations of the way a Point object may be defined: class Point { int xVal, yVal; public: Point (int x = 0, int y = 0); //... }; Given this constructor, the following definitions are all valid: Point p1; // same as: p1(0, 0) Point p2(10); // same as: p2(10, 0) Point p3(10, 20); Careless use of default arguments can lead to undesirable ambiguity. For example, given the class class Point { int xVal, yVal; public: Point (int x = 0, int y = 0); Point (float x = 0, float y = 0); // polar coordinates
  • 9. For more Https://www.ThesisScientist.com Implicit Member Argument When a class member function is called, it receives an implicit argument which denotes the particular object (of the class) for which the function is invoked. For example, in Point pt(10,20); pt.OffsetPt(2,2); pt is an implicit argument to OffsetPt. Within the body of the member function, one can refer to this implicit argument explicitly as this, which denotes a pointer to the object for which the member is invoked. Using this, OffsetPt can be rewritten as: Point::OffsetPt (int x, int y) { this->xVal += x; // equivalent to: xVal += x; this->yVal += y; // equivalent to: yVal += y; } Use of this in this particular example is redundant. There are, however, programming cases where the use of the this pointer is essential. We will see examples of such cases in Chapter 7, when discussing overloaded operators. The this pointer can be used for referring to member functions in exactly the same way as it is used for data members. It is important to bear in mind, however, that this is defined for use within member functions of a class only. In particular, it is undefined for global functions (including global friend functions). Scope Operator When calling a member function, we usually use an abbreviated syntax. For example: pt.OffsetPt(2,2); // abbreviated form This is equivalent to the full form: pt.Point::OffsetPt(2,2); // full form The full form uses the binary scope operator :: to indicate that OffsetPt is a member of Point. In some situations, using the scope operator is essential. For example, the case where the name of a class member is hidden by a local variable (e.g., member function parameter) can be overcome using the scope operator: class Point { public:
  • 10. Point (int x, int y) { Point::x = x; Point::y = y; } //... private: int x, y; } Here x and y in the constructor (inner scope) hide x and y in the class (outer scope). The latter are referred to explicitly as Point::x and Point::y. A member initialization list There are two ways of initializing the data members of a class. The first approach involves initializing the data members using assignments in the body of a constructor. For example: class Image { public: Image (const int w, const int h); private: int width; int height; //... }; Image::Image (const int w, const int h) { width = w; height = h; //... } The second approach uses a member initialization list in the definition of a constructor. For example: class Image { public: Image (const int w, const int h); private: int width; int height; //... }; Image::Image (const int w, const int h) : width(w), height(h) { //... } The effect of this declaration is that width is initialized to w and height is initialized to h. The only difference between this approach and the previous one is that here members are initialized before the body of the constructor is executed.
  • 11. For more Https://www.ThesisScientist.com A member initialization list may be used for initializing any data member of a class. It is always placed between the constructor header and body. A colon is used to separate it from the header. It should consist of a comma- separated list of data members whose initial value appears within a pair of brackets. Constant Members A class data member may defined as constant. For example: class Image { const int width; const int height; //... }; However, data member constants cannot be initialized using the same syntax as for other constants: class Image { const int width = 256; // illegal initializer! const int height = 168; // illegal initializer! //... }; The correct way to initialize a data member constant is through a member initialization list: class Image { public: Image (const int w, const int h); private: const int width; const int height; //... }; Image::Image (const int w, const int h) : width(w), height(h) { //... } As one would expect, no member function is allowed to assign to a constant data member. A constant data member is not appropriate for defining the dimension of an array data member. For example, in class Set { public: Set (void) : maxCard(10) { card = 0; } //...
  • 12. private: const maxCard; int elems[maxCard]; // illegal! int card; }; the array elems will be rejected by the compiler for not having a constant dimension. The reason for this being that maxCard is not bound to a value during compilation, but when the program is run and the constructor is invoked. Member functions may also be defined as constant. This is used to specify which member functions of a class may be invoked for a constant object. For example, class Set { public: Set (void) { card = 0; } Bool Member (const int) const; void AddElem (const int); //... }; Bool Set::Member (const int elem) const { //... } defines Member as a constant member function. To do so, the keyword const is inserted after the function header, both inside the class and in the function definition. A constant object can only be modified by the constant member functions of the class: const Set s; s.AddElem(10); // illegal: AddElem not a const member s.Member(10); // ok The Static Members A data member of a class can be defined to be static. This ensures that there will be exactly one copy of the member, shared by all objects of the class. For example, consider a Window class which represents windows on a bitmap display: class Window { static Window *first; // linked-list of all windows Window *next; // pointer to next window //... };
  • 13. For more Https://www.ThesisScientist.com Here, no matter how many objects of type Window are defined, there will be only one instance of first. Like other static variables, a static data member is by default initialized to 0. It can be initialized to an arbitrary value in the same scope where the member function definitions appear: Window *Window::first = &myWindow; The alternative is to make such variables global, but this is exactly what static members are intended to avoid; by including the variable in a class, we can ensure that it will be inaccessible to anything outside the class. Member functions can also be defined to be static. Semantically, a static member function is like a global function which is a friend of the class, but inaccessible outside the class. It does not receive an implicit argument and hence cannot refer to this. Static member functions are useful for defining call-back routines whose parameter lists are predetermined and outside the control of the programmer. For example, the Window class might use a call-back function for repainting exposed areas of the window: class Window { //... static void PaintProc (Event *event); // call-back }; Referance Members A class data member may defined as reference. For example: class Image { int width; int height; int &widthRef; //... }; As with data member constants, a data member reference cannot be initialized using the same syntax as for other references: class Image { int width; int height; int &widthRef = width; // illegal! //... }; The correct way to initialize a data member reference is through a member initialization list: class Image {
  • 14. public: Image (const int w, const int h); private: int width; int height; int &widthRef; //... };
  • 15. For more Https://www.ThesisScientist.com Class Object Members A data member of a class may be of a user-defined type, that is, an object of another class. For example, a Rectangle class may be defined using two Point data members which represent the top-left and bottom-right corners of the rectangle: class Rectangle { public: Rectangle (int left, int top, int right, int bottom); //... private: Point topLeft; Point botRight; }; The constructor for Rectangle should also initialize the two object members of the class. Assuming that Point has a constructor, this is done by including topLeft and botRight in the member initialization list of the constructor for Rectangle: Rectangle::Rectangle (int left, int top, int right, int bottom) : topLeft(left,top), botRight(right,bottom) { } If the constructor for Point takes no parameters, or if it has default arguments for all of its parameters, then the above member initialization list may be omitted. Of course, the constructor is still implicitly called. The order of initialization is always as follows. First, the constructor for topLeft is invoked, followed by the constructor for botRight, and finally the constructor for Rectangle itself. Object destruction always follows the opposite direction. First the destructor for Rectangle (if any) is invoked, followed by the destructor for botRight, and finally for topLeft. The reason that topLeft is initialized before botRight is not that it appears first in the member initialization list, but because it appears before botRight in the class itself. Therefore, defining the constructor as follows would not change the initialization (or destruction) order: Rectangle::Rectangle (int left, int top, int right, int bottom) : botRight(right,bottom), topLeft(left,top) { }
  • 16. Object Arrays An array of a user-defined type is defined and used much in the same way as an array of a built-in type. For example, a pentagon can be defined as an array of 5 points: Point pentagon[5]; This definition assumes that Point has an ‘argument-less’ constructor (i.e., one which can be invoked without arguments). The constructor is applied to each element of the array. The array can also be initialized using a normal array initializer. Each entry in the initialization list would invoke the constructor with the desired arguments. When the initializer has less entries than the array dimension, the remaining elements are initialized by the argument-less constructor. For example, Point pentagon[5] = { Point(10,20), Point(10,30), Point(20,30), Point(30,20) }; initializes the first four elements of pentagon to explicit points, and the last element is initialized to (0,0). When the constructor can be invoked with a single argument, it is sufficient to just specify the argument. For example, Set sets[4] = {10, 20, 20, 30}; is an abbreviated version of: Set sets[4] = {Set(10), Set(20), Set(20), Set(30)}; An array of objects can also be created dynamically using new: Point *petagon = new Point[5]; When the array is finally deleted using delete, a pair of [] should be included: delete [] pentagon; // destroys all array elements Unless the [] is included, delete will have no way of knowing that pentagon denotes an array of points and not just a single point. The destructor (if any) is applied to the elements of the array in reverse order before the array is deleted. Omitting the [] will cause the destructor to be applied to just the first element of the array: delete pentagon; // destroys only the first element!
  • 17. For more Https://www.ThesisScientist.com Since the objects of a dynamic array cannot be explicitly initialized at the time of creation, the class must have an argument-less constructor to handle the implicit initialization. When this implicit initialization is insufficient, the programmer can explicitly reinitialize any of the elements later: pentagon[0].Point(10, 20); pentagon[1].Point(10, 30); //...
  • 18. Structures and Unions A structure is a class all of whose members are by default public. (Remember that all of the members of a class are by default private.) Structures are defined using the same syntax as classes, except that the keyword struct is used instead of class. For example, struct Point { Point (int, int); void OffsetPt (int, int); int x, y; }; is equivalent to: class Point { public: Point (int, int); void OffsetPt (int, int); int x, y; }; The struct construct originated in C, where it could only contain data members. It has been retained mainly for backward compatibility reasons. In C, a structure can have an initializer with a syntax similar to that of an array. C++ allows such initializers for structures and classes all of whose data members are public: class Employee { public: char *name; int age; double salary; }; Employee emp = {"Jack", 24, 38952.25}; The initializer consists of values which are assigned to the data members of the structure (or class) in the order they appear. This style of initialization is largely superseded by constructors. Furthermore, it cannot be used with a class that has a constructor. A union is a class all of whose data members are mapped to the same address within its object (rather than sequentially as is the case in a class). The size of an object of a union is, therefore, the size of its largest data member. The main use of unions is for situations where an object may assume values of different types, but only one at a time. For example, consider an interpreter for a simple programming language, called P, which supports a number of data types such as: integers, reals, strings, and lists. A value in this language may be defined to be of the type:
  • 19. For more Https://www.ThesisScientist.com union Value { long integer; double real; char *string; Pair list; //... }; where Pair is itself a user-defined type for creating lists: class Pair { Value *head; Value *tail; //... }; Assuming that a long is 4 bytes, a double 8 bytes, and a pointer 4 bytes, an object of type Value would be exactly 8 bytes, i.e., the same as the size of a double or a Pair object (the latter being equal to two pointers). An object in P can be represented by the class, class Object { private: enum ObjType {intObj, realObj, strObj, listObj}; ObjType type; // object type Value val; // object value //... }; where type provides a way of recording what type of value the object currently has. For example, when type is set to strObj, val.string is used for referring to its value. Because of the unique way in which its data members are mapped to memory, a union may not have a static data member or a data member which requires a constructor. Like a structure, all of the members of a union are by default public. The keywords private, public, and protected may be used inside a struct or a union in exactly the same way they are used inside a class for defining private, public, and protected members.
  • 20. Bit Fields It is sometimes desirable to directly control an object at the bit level, so that as many individual data items as possible can be packed into a bit stream without worrying about byte or word boundaries. For example, in data communication, data is transferred in discrete units called packets. In addition to the user data that it carries, each packet also contains a header which is comprised of network-related information for managing the transmission of the packet across the network. To minimize the cost of transmission, it is desirable to minimize the space taken by the header. Figure 6.1 illustrates how the header fields are packed into adjacent bits to achieve this. Figure 6.1 Header fields of a packet. type acknowledge channel sequenceNo moreData These fields can be expressed as bit field data members of a Packet class. A bit field may be defined to be of type int or unsigned int: typedef unsigned int Bit; class Packet { Bit type : 2; // 2 bits wide Bit acknowledge : 1; // 1 bit wide Bit channel : 4; // 4 bits wide Bit sequenceNo : 4; // 4 bite wide Bit moreData : 1; // 1 bit wide //... }; A bit field is referred to in exactly the same way as any other data member. Because a bit field does not necessarily start on a byte boundary, it is illegal to take its address. For the same reason, a bit field cannot be defined as static. Use of enumerations can make working with bit fields easier. For example, given the enumerations enum PacketType {dataPack, controlPack, supervisoryPack}; enum Bool {false, true}; we can write: Packet p; p.type = controlPack; p.acknowledge = true;