Namespaces
Names in C++ can refer to variables, functions, structures, enumerations, classes, and class
and structure members.When programming projects grow large, the potential for name
conflicts increases.When you use class libraries from more than one source, you can get
name conflicts. For example, two libraries might both define classes named List, Tree,
and Node, but in incompatible ways.You might want the List class from one library and
the Tree from the other, and each might expect its own version of Node. Such conflicts
are termed namespace problems.
The C++ Standard provides namespace facilities to provide greater control over the
scope of names. It took a while for compilers to incorporate namespaces, but, by now,
support has become common.
Traditional C++ Namespaces
Before looking at the new namespace facilities in C++, let’s review the namespace properties
that already exist in C++ and introduce some terminology.
*Declarative region :- A declarative region is a region
in which declarations can be made. For example, you can declare a global variable outside
any function.The declarative region for that variable is the file in which it is declared. If
you declare a variable inside a function, its declarative region is the innermost block in
which it is declared.
*potential scope :- The potential scope for a variable
begins at its point of declaration and extends to the end of its declarative region. So
the potential scope is more limited than the declarative region because you can’t use a
variable above the point where it is first defined.
C++’s rules about global and local variables define a kind of namespace hierarchy. Each
declarative region can declare names that are independent of names declared in other
declarative regions.A local variable declared in one function doesn’t conflict with a local
variable declared in a second function.
New Namespace Features
C++ now adds the ability to create named namespaces by defining a new kind of declarative
region, one whose main purpose is to provide an area in which to declare names.The
names in one namespace don’t conflict with the same names declared in other namespaces,
and there are mechanisms for letting other parts of a program use items declared in
a namespace.The following code, for example, uses the new keyword namespace to create
two namespaces, Jack and Jill:
namespace Jack
{
double pail; // variable declaration
void fetch(); // function prototype
int pal; // variable declaration
struct Well { ... }; // structure declaration
}
namespace Jill
{
double bucket(double n) { ... } // function definition
double fetch; // variable declaration
int pal; // variable declaration
struct Hill { ... }; // structure declaration
}
Namespaces can be located at the global level or inside other namespaces, but they
cannot be placed in a block.Thus, a name declared in a namespace has external linkage by
default (unless it refers to a constant).
In addition to user-defined namespaces, there is one more namespace, the global namespace.
This corresponds to the file-level declarative region, so what used to be termed global
variables are now described as being part of the global namespace.
The names in any one namespace don’t conflict with names in another namespace.
Thus, the fetch in Jack can coexist with the fetch in Jill, and the Hill in Jill can
coexist with an external Hill.The rules governing declarations and definitions in a namespace
are the same as the rules for global declarations and definitions.
The simplest way is to use ::, the scope-resolution operator, to qualify a name with its namespace:
Jack::pail = 12.34; // use a variable
Jill::Hill mole; // create a type Hill structure
Jack::fetch(); // use a function
A Namespace Example
// namesp.h *This program is executed in dev-c++
#include < string >
#include < iostream >
// create the pers and debts namespaces
namespace pers
{
struct Person
{
std::string fname;
std::string lname;
};
void getPerson(Person &);
void showPerson(const Person &);
}
namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt(Debt &);
void showDebt(const Debt &)
;
double sumDebts(const Debt ar[], int n);
}
C++ with OOPS
You can declare a name as a class, struct, or union without providing its full definition. This incomplete class declaration lets you use the class name in pointers and references but not in any context in which the full definition is needed. You need a complete class definition when declaring a nonpointer or nonreference object, when using members of the class, and so on. An incomplete class declaration has a number of uses:
Example:-
#include< iostream.h >
#include< conio.h >
class GradeBook
{
public:
// function that displays a welcome message to the E-Education user
void displayMessage()
{
cout << "Welcome to the E-Education!" << endl;
} // end function displayMessage
};
int main()
{
GradeBook myGradeBook; // create a GradeBook object named myGradeBook
myGradeBook.displayMessage(); // call object's displayMessage function
getch();
return 0; // indicate successful termination
}
Output :- Welcome to the E-Education!
Access Specifiers
Access specifiers restrict who can access a member. You can use an access specifier before a base-class name in a class definition and have access specifier labels within a class definition. The access specifiers are:
Public
Anyone can access a public member.
Protected
Only the class, derived classes, and friends can access protected members.
Private
Only the class and friends can access private members.
In a class definition, the default access for members and base classes is private. In a struct definition, the default is public. That is the only difference between a class and a struct, although by convention, some programmers use struct only for POD classes and use class for all other classes.
The access level of a base class affects which members of the base class are accessible to users of a derived class (not the derived class's access to the base class). The access level caps the accessibility of inherited members. In other words, private inheritance makes all inherited members private in the derived class. Protected inheritance reduces the accessibility of public members in the base class to protected in the derived class. Public inheritance leaves all accessibility as it is in the base class.
The access level of a base class also limits type casts and conversions. With public inheritance, you can cast from a derived class to a base class in any function. Only derived classes and friends can cast to a protected base class. For private inheritance, only the class that inherits directly and friends can cast to the base class.
Access level labels in a class definition apply to all data members, member functions, and nested types. A label remains in effect until a new access label is seen or the class definition ends.
Access specifier labels affect the layout of nonstatic data members. In the absence of access specifier labels, nonstatic data members are at increasing addresses within an object in the order of declaration. When separated by access specifier labels, however, the order is implementation-defined.
interface
An interface is a shared framework for interactions between two systems—for instance,
between a computer and a printer or between a user and a computer program. For example,
the user might be you and the program might be a word processor. When you use the word
processor, you don’t transfer words directly from your mind to the computer memory.
Instead, you interact with the interface provided by the program. You press a key, and the
computer shows you a character on the screen. You move the mouse, and the computer
moves a cursor on the screen. You click the mouse accidentally, and something weird happens
to the paragraph you were typing. The program interface manages the conversion of
your intentions to specific information stored in the computer.
For classes, we speak of the public interface. In this case, the public is the program using
the class, the interacting system consists of the class objects, and the interface consists of
the methods provided by whoever wrote the class. The interface enables you, the programmer,
to write code that interacts with class objects, and thus it enables the program to use
the class objects. For example, to find the number of characters in a string object, you
don’t open up the object to what is inside; you just use the size() method provided by the
class creators. It turns out that the class design denies direct access to the public user.
But the public is allowed to use the size() method. The size() method, then, is part of
the public interface between the user and a string class object. Similarly, the getline()
method is part of the iostream class public interface; a program using cin doesn’t tinker
directly with the innards of a cin object to read a line of input; instead, getline() does
the work.
If you want a more personal relationship, instead of thinking of the program using a class
as the public user, you can think of the person writing the program using the class as the
public user. But in any case, to use a class, you need to know its public interface; to write a
class, you need to create its public interface.
Member Access Control: Public or Private?
You can declare class members, whether they are data items or member functions, either
in the public or the private section of a class. But because one of the main precepts of
OOP is to hide the data, data items normally go into the private section.The member
functions that constitute the class interface go into the public section; otherwise, you can’t
call those functions from a program.As the Stock declaration shows, you can also put
member functions in the private section.You can’t call such functions directly from a program,
but the public methods can use them.Typically, you use private member functions
to handle implementation details that don’t form part of the public interface.
#include< iostream.h >
#include< conio.h >
class addition
{
int a,b,c;
public:
void add()
{
a=15;
b=20;
c=a+b;
}
void display()
{
cout<< a<<"+"<
}};
void main()
{
clrscr();
addition add1;
add1.add();
add1.display();
getch();
}
Classes & Objects in Detail: -
Concept
Description
Class member
functions
A member function of
a class is a function that has its definition or its prototype
within the class definition like any other variable.
Class access
modifiers
A class member can be
defined as public, private or protected. By default
members would be assumed as private.
Constructor &
Destructor
A class constructor
is a special function in a class that is called when a new object of
the class is created. A destructor is also a special function which
is called when created object is deleted.
C++ copy
Constructor
The copy constructor
is a constructor which creates an object by initializing it with an
object of the same class.
C++ friend
functions
A friend function is permitted full access to private and
protected members of a class.
C++ Inline
Functions
With an inline
function, the compiler tries to expand the code in the body of the
function in place of a call to the function.
The this pointers
in c++
Every object has a
special pointer this
which points to the object itself.
Pointers to c++
classes
A pointer to a class
is done exactly the same way a pointer to a structure is. In fact a
class is really just a structure with functions in it.
Static members of a class
Both data members and
function members of a class can be declared as static.