Constructor PPT Updated
-
Upload
sourabh-agrawal -
Category
Documents
-
view
229 -
download
0
Transcript of Constructor PPT Updated
-
7/31/2019 Constructor PPT Updated
1/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Effective CPP
Sourabh Agrawal11-Aug-2011
-
7/31/2019 Constructor PPT Updated
2/25
5/26/12 Sourabh Agrawal
AGENDA To suggest a approach to effective way of writing C++
which are more comprehensible, maintainable, portable,extensible, efficient, and likely to behave as you expect .
-
7/31/2019 Constructor PPT Updated
3/25
5/26/12 Sourabh Agrawal
Topics Know what functions C++ silently writes and calls
Prefer initialization to assignment in constructors
List members in an initialization list in the order in which they aredeclared
Prevents Constructor from being used to perform implicit typeconversions
Declare a copy constructor and an assignment operator for classeswith dynamically allocated memory
Declare destructors virtual in polymorphic base classes
-
7/31/2019 Constructor PPT Updated
4/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Functions C++ silentlywrites and calls in a
Class
-
7/31/2019 Constructor PPT Updated
5/25
5/26/12 Sourabh Agrawal
class Empty
{
};
it's essentially the same as if you'dwritten this:
class Empty
{public:
Empty() { }
~Empty() { }
-
7/31/2019 Constructor PPT Updated
6/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Prefer initialization toassignment in
constructors
-
7/31/2019 Constructor PPT Updated
7/25
5/26/12 Sourabh Agrawal
Class A{
public:A (string initName ){
name = initName;
}
private:string name;};
-
7/31/2019 Constructor PPT Updated
8/25
5/26/12 Sourabh Agrawal
Class A{
public:A (string initName ): name
(initName)
{
}
private:string name;};
-
7/31/2019 Constructor PPT Updated
9/25
5/26/12 Sourabh Agrawal
Const and Reference class member
variables must be initialized inconstructor.
-
7/31/2019 Constructor PPT Updated
10/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
List members in the classin its dependency order
-
7/31/2019 Constructor PPT Updated
11/25
5/26/12 Sourabh Agrawal
template
class Array
{public:
Array(int low, int high);//Constructor Declaration
size_t size; // No. of elements in array
vector data; // thearray data is stored
int lBound, hBound; // lower
bound, higher bound
-
7/31/2019 Constructor PPT Updated
12/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Prevents Constructorfrom being used to
perform implicit typeconversions
-
7/31/2019 Constructor PPT Updated
13/25
5/26/12 Sourabh Agrawal
class ABC
{
public:ABC(int x)
{
}
};
void function(ABC Object)
{
cout
-
7/31/2019 Constructor PPT Updated
14/25
5/26/12 Sourabh Agrawal
class ABC
{
public:explicit ABC(int x) // Not allowing
implicit casting of integer to ABC
{}
};
void function(ABC Object)
{
-
7/31/2019 Constructor PPT Updated
15/25
5/26/12 Sourabh Agrawal
Declare a copyconstructor and an
assignment operator for
classes with dynamicallyallocated memory
l S i
-
7/31/2019 Constructor PPT Updated
16/25
5/26/12 Sourabh Agrawal
class String
{
char *data;
public:
String::String(const char *value)
{
if (value)
{
data = new char[strlen(value) + 1];
strcpy(data, value);
}
}
String::~String(){
delete [] data;
}
};int main()
BitwiseCopying
BitwiseCopying
-
7/31/2019 Constructor PPT Updated
17/25
5/26/12 Sourabh Agrawal
String a("Hello");String b("World");
b = a; // Both b and a data* are pointing to samememory location
Memory Leak
Leads to MemoryCorru tion
deletea
-
7/31/2019 Constructor PPT Updated
18/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Declare destructorsvirtual in polymorphic
base classes
-
7/31/2019 Constructor PPT Updated
19/25
5/26/12 Sourabh Agrawal
Acc. to C++ Standard:
When you try to delete a derived
class object through a base classpointer and the base class has a non-virtual the results are undefined.
Polymorphic base classes shoulddeclare virtual destructors. If a classhas any virtual functions, it shouldhave a virtual destructor.
-
7/31/2019 Constructor PPT Updated
20/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Handle assignment to
self in operator=
-
7/31/2019 Constructor PPT Updated
21/25
5/26/12 Sourabh Agrawal
If you can detect an assignment toself at the top of your assignmentoperator(s), you can return rightaway, possibly saving a lot of workthat you'd otherwise have to gothrough to implement assignment
A more important reason forchecking for assignment to self is toensure correctness.
-
7/31/2019 Constructor PPT Updated
22/25
5/26/12 Sourabh Agrawal
Have operator= return areference to *this
-
7/31/2019 Constructor PPT Updated
23/25
5/26/12 Sourabh Agrawal
Prevent exceptions fromleaving destructors
Destructors should never emitexceptions. If functions called in adestructor may throw, the destructorshould catch any exceptions, thenswallow them or terminate theprogram.
If class clients need to be able toreact to exceptions thrown during anoperation, the class should provide a
-
7/31/2019 Constructor PPT Updated
24/25
5/26/12 Sourabh Agrawal
functions duringconstruction or
destructionDon't call virtual functions during
construction or destruction, becausesuch calls will never go to a morederived class than that of the currentlyexecuting constructor or destructor.
-
7/31/2019 Constructor PPT Updated
25/25
Click to edit Master subtitle style
5/26/12 Sourabh Agrawal
Thank You !!!
References:Effective C++C++ Standard Draft