Академический Документы
Профессиональный Документы
Культура Документы
An object of a derived class is a kind of the base class. Therefore the conversion
from a derived class pointer to a base class pointer is perfectly safe, and
happens all the time. For example, if I am pointing at a car, I am in fact pointing
at a vehicle, so converting a Car* to a Vehicle* is perfectly safe and normal:
3. References
An alias (an alternate name) for an object. References are frequently used for
pass-by-reference:
void swap(int& i, int& j) {
int tmp = i;
i = j;
j = tmp;
}
int main()
{
int x, y;
...
swap(x,y);
...
}
Here i and j are aliases for main's x and y respectively. In other words, i is x —
not a pointer to x, nor a copy of x, but x itself. Anything you do to i gets done to x, and
vice versa. OK. That's how you should think of references as a programmer. Now, at the
risk of confusing you by giving you a different perspective, here's how references are
implemented. Underneath it all, a reference i to object x is typically the machine address
of the object x. But when the programmer says i++, the compiler generates code that
increments x. In particular, the address bits that the compiler uses to find x are not
changed. A C programmer will think of this as if you used the C style pass-by-pointer,
with the syntactic variant of (1) moving the & from the caller into the callee, and (2)
eliminating the *s. In other words, a C programmer will think of i as a macro for (*p),
where p is a pointer to x (e.g., the compiler automatically dereferences the underlying
pointer; i++ is changed to (*p)++; i = 7 is automatically changed to *p = 7).
4. How can you reseat a reference to make it refer to a different object?
References are usually preferred over pointers whenever you don't need "reseating".
This usually means that references are most useful in a class's public interface.
References typically appear on the skin of an object, and pointers on the inside.
It means x aliases a Fred object, but x can't be used to change that Fred object.
No, To find out what the above declaration means, you have to read it right-to-left. Thus
"Fred& const x" means "x is a const reference to a Fred". But that is redundant, since
references are always const. You can't reseat a reference. Never. With or without the
const.
Because you must explicitly define your class's static data members.
Fred.h:
class Fred {
public:
Fred();
...
private:
int i_;
static int j_;
};
Fred.cpp (or Fred.C or whatever):
Fred::Fred()
: i_(10) // OK: you can (and should) initialize member data this way
, j_(42) // Error: you cannot initialize static member data like this
{
...
}
// You must define static data members this way:
int Fred::j_ = 42;
When you declare an inline function, it looks just like a normal function:
void f(int i, char c);
But when you define an inline function, you prepend the function's definition with
the keyword inline, and you put the definition into a header file:
inline
void f(int i, char c)
{
...
}
Note: It's imperative that the function's definition (the part between the {...}) be
placed in a header file, unless the function is used only in a single .cpp file. In
particular, if you put the inline function's definition into a .cpp file and you call it from
some other .cpp file, you'll get an "unresolved external" error from the linker.
Is there another way to tell the compiler to make a member function inline?
Yep: define the member function in the class body itself:
class Fred {
public:
void f(int i, char c)
{
...
}
};
These are ordinary functions defined with a return type and parameters. The return type can
also be void. The special trait about member functions is they can access the private/protected
data members of their class and manipulate them. No external functions can access the
private/protected data members of a class.
Constructors:
Constructors in C++ are special member functions of a class. They have the same name as the
Class Name. There can be any number of overloaded constructors inside a class, provided they
have a different set of parameters. There are some important qualities for a constructor to be
noted.
Destructors in C++ also have the same name, except for the fact that they are preceded by a '~'
operator. The destructors are called when the object of a class goes out of scope. It is not
necessary to declare a constructor or a destructor inside a class. If not declared, the compiler will
automatically create a default one for each. If the constructor/destructor is declared as private,
then the class cannot be instantiated.
Access Level:
The classes in C++ have 3 important access levels. They are Private, Public and Protected.
The explanations are as follows.
Private:
The members are accessible only by the member functions or friend functions.
Protected:
These members are accessible by the member functions of the class and the classes which are
derived from this class.
Public:
Function overloading
Function overloading is the practice of declaring the same function with different
signatures. The same function name will be used with different number of parameters and
parameters of different type. But overloading of functions with different return types are not
allowed.
When a function is declared inline, the function is expanded at the calling block. The
function is not treated as a separate unit like other normal functions.
But a compiler is free to decide, if a function qualifies to be an inline function. If the inline
function is found to have larger chunk of code, it will not be treated as an inline function, but as
like other normal functions.
Inline functions are treated like macro definitions by the C++ compiler.
In fact, the keyword inline is not necessary. If the function is defined with its body directly
and the function has a smaller block of code, it will be automatically treated as inline by the
compiler.
Static Functions
Static data types can be accessed without instantiation of the class in C++. This is
applicable for static functions also.
The differences between a static member function and non-static member functions are
as follows.
• A static member function can access only static member data, static member functions
and data and functions outside the class. A non-static member function can access all of
the above including the static data member.
• A static member function can be called, even when a class is not instantiated, a non-
static member function can be called only after instantiating the class as an object.
• A static member function cannot be declared virtual, whereas a non-static member
functions can be declared as virtual
• A static member function cannot have access to the 'this' pointer of the class.
The static member functions are not used very frequently in programs. But nevertheless, they
become useful whenever we need to have functions which are accessible even when the class is
not instantiated.
The this pointer is used as a pointer to the class object instance by the member function. The
address of the class instance is passed as an implicit parameter to the member functions.
The sample below, in this c++ Tutorial shows how to use it. It is a common knowledge
that C++ keeps only one copy of each member function and the data members are
allocated memory for all of their instances. This kind of various instances of data are
maintained use this pointer. Look at the sample below, in this c++ Tutorial.
• this pointer stores the address of the class instance, to enable pointer access of the
members to the member functions of the class.
• this pointer is not counted for calculating the size of the object.
• this pointers are not accessible for static member functions.
• this pointers are not modifiable.
friend
A class can allow non-member functions and other classes to access its own private
data, by making them as friends. This part of C++ tutorial essentially gives two important points.
• Once a non-member function is declared as a friend, it can access the private data of the
class
• similarly when a class is declared as a friend, the friend class can have access to the
private data of the class which made this a friend
#include < iostream.h >
class CPP_Tutorial
{
int private_data;
friend class friendclass;
public:
CPP_Tutorial()
{
private_data = 5;
}
};
class friendclass
{
public:
int subtractfrom(int x)
{
CPP_Tutorial var2;
return var2.private_data - x;
}
};
int main()
{
friendclass var3;
cout << "Added Result for this C++ tutorial: "<< var3.subtractfrom(2)<
} The output of the above C++ Tutorial sample will be
Subtracted Result for this C++ tutorial: 3
inheritance
Creating or deriving a new class using another class as a base is called inheritance in C+
+. The new class created is called a Derived class and the old class used as a base is called a
Base class in C++ inheritance terminology.
The derived class will inherit all the features of the base class in C++ inheritance. The
derived class can also add its own features, data etc., It can also override some of the features
(functions) of the base class, if the function is declared as virtual in base class.
C++ inheritance is very similar to a parent-child relationship. When a class is inherited
all the functions and data member are inherited, although not all of them will be accessible by the
member functions of the derived class. But there are some exceptions to it too.
Some of the exceptions to be noted in C++ inheritance are as follows.
A constructor cannot be virtual because at the time when the constructor is invoked the
virtual table would not be available in the memory. Hence we cannot have a virtual
constructor.
A virtual destructor is one that is declared as virtual in the base class and is used
to ensure that destructors are called in the proper order. It is to be remembered that
destructors are called in the reverse order of inheritance. If a base class pointer points to
a derived class object and we some time later use the delete operator to delete the
object, then the derived class destructor is not called.
#include <iostream.h>
class base
{
public:
virtual void display()
{
cout<<”\nBase”;
}
};
class derived : public base
{
public:
void display()
{
cout<<”\nDerived”;
}
};
void main()
{
A constructor cannot be virtual because at the time when the constructor is invoked the
virtual table would not be available in the memory. Hence we cannot have a virtual
constructor.
A virtual destructor is one that is declared as virtual in the base class and is used
to ensure that destructors are called in the proper order. It is to be remembered that
destructors are called in the reverse order of inheritance. If a base class pointer points to
a derived class object and we some time later use the delete operator to delete the
object, then the derived class destructor is not called. Refer to the code that follows:
#include <iostream.h>
class base
{
public:
~base()
{
}
};
void main()
{
}
};
};
void main()
{
Hashtable is synchronized means it's therad safe where as hashmap is not and
hash table can't store null value hashmap can store null values. HashTable is thread
safe is because it makes lock in it when it executes get() or put() methods. It is good one
when you use the data in an multi-threading application. You may try use hashmap
inside synchronized block.
Arrays can be expected to have the fastest access (primitives functions) and
least amount of memory usage. However they are fixed in size.
if you have the index, using a standard array is faster in all cases where you can
fit the whole array in memory.
Maps are good for associated arrays (e.g. where the index is a string), for sparse
arrays (where only a few elements are used, but the index range is large), etc.
Using maps for sparse arrays generally uses (much) less memory than a normal
array.
Placement new
What is the "placement new" operator in C++?
Placement new
Operator new allocates memory from the heap, on which an object is constructed.
Standard C++ also supports placement new operator, which constructs an object on a
pre-allocated buffer. This is useful when building a memory pool, a garbage collector or
simply when performance and exception safety are paramount (there's no danger of
allocation failure since the memory has already been allocated, and constructing an
object on a pre-allocated buffer takes less time):
void placement() {
But internally both new and delete make use of malloc and free.
new generates exception in case of a failure