Beruflich Dokumente
Kultur Dokumente
Introduction
Classes are to C++ what structures are to C. Both provide the library programmer a
means to create new data types.
Problem: In C, the absence of a facility to bind the data and the code that can
have the exclusive rights to manipulate the data can lead to difficult-to-detect runtime bugs. (See example).
Two new keywords, private and public have been introduced in the definition of
the structure.
The keywords private and public are also known as access modifiers or access
specifiers because they control the access to the members of structures.
They help give exclusive rights to the member functions to work upon the member
data. One can specify member functions as public but member data as private.
Any attempt to access/modify a private member of a structure variable by a nonmember function will be prevented by the C++ compiler. Chapter02\L2P3.txt
Since the member functions are public, they can be invoked from any part of the
program.
Structure members are public by default. Chapter02\L2P4.txt
Class members are private by default. Chapter02\L2P5.txt
3
Objects:
Step 3 It puts the definition of the this pointer as a leading formal argument in the
definitions of all member functions. It also modifies all the statements to access
Object members by accessing them through the this pointer using the pointer-tomember access operator (->). Chapter02\example-pg54.txt
Step 4 It passes the address of invoking object as a leading parameter to each call
to the member functions. Chapter02\example-pg-55.txt
Members of the invoking object are referred to when they are accessed without
any qualifiers in member functions.
The accessibility of the implicit object is the same as that of the other objects
passed as parameters in the function call and the local objects inside that
function.
6
Data Abstraction
Data Abstraction
void distance::setinches(float y)
{
finches=y;
if (finches>=12)
{
ifeet+=(int)finches/12;
finches-=((int)finches/12)*12;
}
}
distance distance::add(distance y)
{
distance t;
t.ifeet=ifeet+y.ifeet;
t.finches=setinches(finches+y.finches);
return t;
}
10
13
Friend Functions
A few points about the friend functions are as follows:
friend keyword should appear in the prototype only and not in the definition.
Since it is a non-member function of the class of which it is a friend, it can be
prototyped in either the private or the public section of the class.
A friend function takes one extra parameter as compared to a member function
that performs the same task. This is because it cannot be called with respect to
any object. Instead, the object itself appears as an explicit parameter in the
function call.
The scope resolution operator should not be used while defining a friend function.
There are situations where a function that needs to access the private data members
of the objects of a class and cannot be called with respect to an object of the
class. In such situations, the function must be declared as a friend.
15
Friend Classes
A class can be a friend of another class. Member functions of a friend class can
access private data members of objects of the class of which it is a friend. (See
Listing 2.23 and Listing 2.24). Chapter02\L2P23.txt Chapter02\L2P24.txt
Friendship is not transitive. (See Listing 2.25).Chapter02\L2P25.txt
Friend Member Functions These are used when one wants to make some specific
member functions of one class friendly to another class. (See Example on page 57)
For eg., if the function test_frnd() of class B must be the friend of class A then ,
replace
friend class B; by
friend void B::test_frnd(); //in class A
Any problem of circular dependence is solved by forward declaration i.e. forward
declare a class that requires a friend. (See Listing 2.26).Chapter02\L2P26.txt
16
Problem: Declaring a friend member function inline as the compiler will not know
that there is a data member x inside the definition of a friend class. (See Listing
2.27).Chapter02\L2P27.txt
Solution: Define it as inline after the definition of the friend class in the header file
itself. (See Listing 2.28). Chapter02\L2P28.txt
Friends as bridges
Friend functions can be used as bridges between two classes.
Suppose there are two unrelated classes whose private data members need a
simultaneous update through a common function. This function should be
declared as a friend to both the classes. (See Listing 2.29).Chapter02\L2P29.txt
17
Q1. Define 2 classes namely: XYZ and ABC each having integer data
members. Write a member function setval() to assign values to integer
members under both classes. Write a friend function max() which
computes the maximum of 2 integer members passing XYZ and ABC
objects as arguments. Write a main () to test the classes.
For example, it may be used for a variable within a class that can
contain a counter with the number of objects of that class that
have been created, as in the following example:
#include <iostream>
class item
{ static int count;
int number;
public :
void setdata(int a)
{ number =a;
count++;
}
void getcount(void)
{ cout<< count:;
cout<< count; } };
void main()
{
item a,b,c; // count is initialized to zero
a.getcount();//display count
b.getcount();
c.getcount();
a.setdata(100);
b.setdata(200);
c.setdata(300);
cout<<after reading the data\n;
a.getcount();
b.getcount();
c.getcount();
}
Output:
count: 0
count: 0
count: 0
After reading the data
count: 3
count: 3
count: 3
26
Namespaces
Namespaces
The using directive brings back the global namespace pollution that the
namespaces mechanism was supposed to remove in the first place.
Problem: Some namespaces have long names. Qualifying the name of a class
that is enclosed within such a namespace, with the name of the namespace, is
cumbersome. Chapter02\L2P46.txt
Solution: Assigning a suitably short alias to such a long namespace name solves
the problem. Chapter02\L2P47.txt
Aliases provide an incidental benefit also. Suppose an alias has been used at a
number of places in the source code. Changing the alias declaration so that it
stands as an alias for a different namespace will make each reference of the
enclosed class refer to a completely different class.
29
#include <iostream>
namespace MyNames
{
int value1 = 10;
int value2 = 20;
int ComputeSum()
{
return (value1 + value2);
}
}
int main()
{
std::cout << MyNames::ComputeSum() << std::endl;
}
If you use multiple using namespace statements, you may get a compile-time
error due to ambiguity:
#include <iostream>
namespace MyNames
{
int value1 = 10;
int value2 = 20;
}
namespace MyOtherNames
{
int value1 = 30;
int value2 = 40;
}
using namespace std;
using namespace MyNames;
using namespace MyOtherNames;
int main()
{
value1 = 50;
cout<<value1;
//Namespace Example
namespace Box1
{
int boxSide = 4;
}
namespace Box2
{
int boxSide = 12;
}
int main ()
{
int boxSide = 42;
cout << Box1::boxSide <<
endl; cout << Box2::boxSide <<
endl; cout << boxSide << endl;
return 0;
}
A class can be defined inside another class. Such a class is known as a nested
class.
The class that contains the nested class is known as the enclosing class.
Nested classes can be defined in the private, protected, or public portions of the
enclosing class. Chapter02\L2P48.txt Chapter02\L2P49.txt
A nested class is created if it does not have any relevance outside its enclosing
class. By defining the class as a nested class, we avoid a name collision.
The size of objects of an enclosing class is not affected by the presence of nested
classes. Chapter02\L2P50.txt
Even if there is a class B defined as global in the same file, its name will not clash
with the nested class B.
33
Member functions of the nested class can access the non-static public members
of the enclosing class through an object, a pointer, or a reference
only.Chapter02\L2P54\enclClassObj2.txt .Chapter02\L2P54\enclClassObj.txt
An error is produced when a direct access is made to a member of the enclosing
class through a function of the nested class. This is because creation of an object
of the nested class does not cause an object of the enclosing class to be created.
The classes are nested to merely control the visibility.
By default, the enclosing class and the nested class do not have any access
rights to each others private data members. They can do so only if they are
friends to each other.
35