Programming paradigms Programming paradigms means the way of writing a program depending upon the...

77
Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task of development team to develop programs for the small-scale projects to the large-scale projects. The programming paradigms is categorized into the following way 1. Procedural programming 2. Structural programming 3. Object oriented programming

Transcript of Programming paradigms Programming paradigms means the way of writing a program depending upon the...

Page 1: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Programming paradigms

Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task of development team to develop programs for the small-scale projects to the large-scale projects.

The programming paradigms is categorized into the following way1. Procedural programming2. Structural programming3. Object oriented programming

Page 2: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 3: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• The following are the important features:• Programs are organized in the form of subroutines and all data items are global• Program control achieved by call to subroutines• Code reusability occurs• Suitable for medium sized software application• Difficult to maintain and enhance the program code

• The drawback is there is no data security for global data.

• Advantages of procedural programming include its relative simplicity, and ease of implementation of compilers and interpreters.

• Examples of procedural programming languages include FORTRAN, ALGOL, Pascal, C, MODULA2, Ada, BASIC.

Page 4: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Structured programming• Structured programming is used to develop large-scale projects. The large-scale project

consists of large development team, developing different parts of the same project independently.

• The separately compiled program modules are grouped together to create big projects. In this multiple module program, each module is having a separate set of related functions. The following are important features of structured programming

Page 5: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Importance given to algorithm rather than data• Projects can be broken up into modules and programmed independently• Each module is divided into individual procedures that perform separate tasks.• Module are independent of each other as far as possible• Modules have their own local data and processing logic• User defined data types are introduced.•

Page 6: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Object oriented programming: OOP is a method of implementation in which programs are organized as co-operative collections of objects, each of which represents an instance of some class and whose classes are all members of a hierarchy of classes united through the property called inheritance.

Page 7: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 8: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Benefits of OOP1. Implementing Reusability by making Reusable Classes and creating Objects from those

Classes.2. Providing various levels of Data protection and hiding by using Public, Private and Protected

access specifiers.3. Providing Protection to Data and Functions both.4. Helps in creating Flexible, Extensible and Maintainable code.5. Effective solutions for Real World problems by implementing Classes representing Real World

Entities.6. Quick software development due to Reusability and easy implementation of Real World

Entities and their Relationships using Inheritance.

Page 9: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• The following are the important features of object-oriented programming.• Importance given to data rather than algorithm• Data abstraction is introduced in addition to procedural abstraction.• Data and associated operations are unified into a single unit • The objects are grouped with common attribute operation and semantics.• Programs are designed around the data being operated rather than operate

themselves.• Relation ships can be created between similar, yet distinct data types.

Page 10: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 11: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 12: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Top-Down programming• In the top-down design the overall task of the program is to break down a large

programs into several logics and then they are interested by calling these logics whenever necessary.

• The top-down approach starts from a high level abstract solution of the given problem and work down toward more detailed specific solution. In this, the over all task is just defined in terms of generalized subtask, which are subsequently further, defined and so on.

• This process is continued downward until the sub tasks are defined in a form suitable for execution by the computer.

Page 13: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 14: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Advantages• It shows the sequential division of the problem from top to bottom, which is a generalized

and easy method.• Modules can be developed parallel.• Easy, quick and minimum error prone development programs.

Page 15: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Bottom-up Approach

• Bottom-up programming is the opposite of top down programming. In bottom up approach all the subtask are first coded into program and then interacted into increasingly larger modules of the design, in the bottom up approach available set of modules are first identified.

• An attempt is made to combine the lower modules/subprograms to form modules of a high level.

• This process of combining modules is continued until the program is constructed. In this approach quite after it is found that the final program obtained by combining the predetermined lowest level modules does not meet all the requirement of the desired program.

• In a language such as “C++” or “java”, bottom up programming takes the form of constructing abstract data types.

Page 16: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Advantages• The most important modules are written and tested first.• It is easier for users or customers to see the progress being made in the project.• Testing and debugging are easier and more efficient.• The implementation is normally smoother and shorter.• Programmer moral and job static function are increased.• It is easier to detect and correct time delay and cost average runs.• It is easier to deal with time delays and cost overruns when they occur.• Data processing resources are used more evenly and efficiently.

• Disadvantages• The basic drawback of the bottom up approach is the assumption that the lowest level

modules can be completely specified beforehand, which in reality is seldom possible.

Page 17: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Basic Concepts of Object Oriented Programming

1. Object2. Class 3. Data Encapsulation and Abstraction4. Data Hiding5. Inheritance6. Polymorphism7. Dynamic binding8. Message Passing

Page 18: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Object• Object: Objects are the basic run-time entities in an object-oriented system.

Programming problem is analyzed in terms of objects and nature of communication between them. When a program is executed, objects interact with each other by sending messages. Different objects can also interact with each other without knowing the details of their data or code.

• An Object is an Instance of a Class. An Instance is the existence in the computer system by acquiring a memory space.

• An Object is the only way a Class becomes usable.

• Objects are basis of Object Oriented Programming.

• An Object has all the Characteristics of the Class using which the Object is created.

• It implements reusability of code written in Classes.

Page 19: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 20: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• 2. Classes: A class is a collection of objects of similar type. Once a class is defined, any number of objects can be created which belong to that class.

• In fact, objects are variables of the type class. Once a class has been defined, we can create any number of objects belonging to that class.

• A Class is a Blueprint or Stencil for creating Objects. • A Class specifies all the Member Data and Member Functions that would be

present in the Objects created using the Class. • Using a Class any number of Objects can be created. • It’s a User Defined Data Type also called Abstract Data Type, which acts like a basic

data type when used. • Usually Classes are used to represent computer understandable formats for Real

World Entities.

Page 21: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Data Abstraction and Encapsulation• Abstraction refers to the act of representing essential features without including the

background details or explanations.• Classes use the concept of abstraction and are defined as a list of abstract attributes. Storing

data and functions in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it.

Abstraction :-• It’s the process of Hiding the complexities of implementation and providing a simple Interface

for easy use. • Its implemented using Encapsulation. • Functions, Structures and Classes implement Abstraction at programmer level.

Page 22: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Encapsulation :- • It’s the process of wrapping up of Data and Functions inside a single Entity (usually

Classes). • Encapsulation helps to establish Abstraction and Data Hiding. • Functions, Structures and Classes implement Encapsulation at programmer level. • 5. Data Hiding :-• It’s the process of keeping the Data under such an Access mode that its only

accessible to permitted Functions.Using Private, Protected specifiers, we can hide Data from access from outside a Class. Functions, Structures and Classes implement Data Hiding at programmer level.

Page 23: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Inheritance• Inheritance is the process by which objects can acquire the properties of objects of

other class. In OOP, inheritance provides reusability, like, adding additional features to an existing class without modifying it. This is achieved by deriving a new class from the existing one. The new class will have combined features of both the classes.

Page 24: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Here the Class which gets the Inherited properties is called the Child Class and from which it acquires the properties is called the Base or Parent Class.

• During Inheritance the Protected and Public Data and Functions get passed on as copies from the Parent or Base Classes to Child Classes.

• It forms a Hierarchical Structure from Parent down to Children Classes. • It implements Reusability of Parent Class Data and Functions. • Structures and Classes can be used to implement Inheritance. •

Page 25: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 26: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Polymorphism• Polymorphism means the ability to take more than one form. An operation may

exhibit different behaviors in different instances. • The behavior depends on the data types used in the operation. • Polymorphism is extensively used in implementing Inheritance using same name

with either Different number of Arguments or Different Data types of Arguments.• Types:- Compile Time (the compiler recognizes the binding between the function

and its code during compile time) and Run Time(the compiler recognizes the binding between the function and its code during runtime)

Page 27: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 28: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Dynamic binding :-• Its also called Runtime Polymorphism, where the compiler recognizes the binding between

the function and its code during runtime. • Its implemented using Virtual Functions, during Inheritance.

Page 29: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Message Passing :- • It’s the process of passing Data between Objects of same Class and also of

different Classes. • Its implemented using Function calling or by simply sharing data. • Types:- From User to Objects (done by invoking Function inside an Object) and

From Object to Object (by sharing Data of one Object with another Object). • Using Friend functions (with objects as input arguments), message passing can be

done between two or more Objects of Different Classes. • Using member Functions (with objects as input arguments), message passing can

be done between objects of same Class.

Page 30: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• History Of The C++: C++ is an Object Oriented Programming laguage.It was developed by Bjarne Stroustrup at AT&T Bell Laboratories in Murray Hill New Jersy, USA, in early 1980’s.

• C++ is an extension of C with major addition of the class construct feature.• Stroustrup initially called the new language’C with Classes.• In 1983 the name was changed to the C++.• C++ is superset of the C.

Page 31: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Structure of the C++ Program :

Page 32: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Simple C++ Program• • // my first program in C++• #include <iostream.h>• void main ()• { • cout << "Hello World!";• }

Page 33: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 34: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Class: Class can be defined as combination of data members and member functions applied on that data. It allows the data to be hidden from the external use. A class can also be called as an abstract data type (ADT).

• Abstract Data Type can be defined as the technique of creating new data types whose implementation details are hidden and can only be handled using the publicly accessible member function

• It’s a User Defined Data-type.• 1.The Data declared in a Class are called Data-Members of the Class.• 2.The Functions declared or defined in a Class are called Member-Functions of the

Class.• The members of a Class can only be accessed through Objects of the class.

Page 35: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Where class_name is the name for a class (user defined type) and the optional field object_name is one, or several, valid object identifiers. The body of the declaration can contain members, which can be either data

members or member functions

Page 36: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• The following are the characteristics of a class• The keyword class specifies abstract data type of type class name.• The body of a class is enclosed with in braces and terminated by a semicolon• The functions and variables with in the class are collectively called as members• The members that have been declared as private can be accessed only from with

in the class. • Class definition is only a template and does not create any memory space for a

class• By default all the members are of type private .•

Page 37: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Three Access Specifiers :-

• Public :- • Any member declared under this specifier is Accessible from Outside the class,

by using the object of the Class.• The Public members of a Class get Inherited completely to the Child Classes. The keyword “ public : “ is used.• Syntax :-

Class definition{ public :declarations or definitions} ;

Page 38: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Example :- class A{ public :

int a;} ;main(){ A oba ;

oba . a = 10 ; // we have set the value of variable ‘ a ‘ in object ‘ oba ‘ to 10

}

Page 39: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Private :-• Any member declared under this specifier is Not Accessible from Outside the

Class. • The Private members are accessible to only the Member Functions and Friend

Functions in the Class.• The Private members of a Class Never get Inherited to the Child Classes. The keyword “ private : “ is used. Syntax :-Class definition{ private :declarations or definitions} ;

Page 40: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Example :- • class A• { private :• int a;• public :• void fill ( )• { cin >> a ;• }• } ;• main()• {• A oba ;• // oba . a = 10 ; // invalid as ‘ a ‘ is private member.• oba . fill ( ) ; // valid as fill ( ) is a public member.• }

Page 41: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Protected :- • Any member declared under this specifier is Not Accessible from Outside the

class, by using the object of the Class.• The Protected members of a Class get Inherited completely to the Child Classes

and they maintain their Protected visibility in the child Class. The Protected members are accessible to only the Member Functions and Friend

Functions in the Class.• The keyword “ protected : “ is used. Syntax :-Class definition{ protected :declarations or definitions} ;

Page 42: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Example :- • class A• { protected :• int a;• public :• void fill ( )• { cin >> a ;• }• } ;• main()• { A oba ;• // oba . a = 10 ; // invalid as ‘ a ‘ is protected member.• oba . fill ( ) ; // valid as fill ( ) is a public member.• }•

Page 43: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Member function defined outside the class definition:• To declare the member function of a class outside the class definition the function

prototype declared within the body of a class and defined them out side the body of a class.

• Member ship identity label (class_name :: ) differentiate member functions and non member function in a program.

• By default the member function defined outside the class definition is non-inline. • To make it inline by explicitly adding inline as prefix to the member function in the

definitio

Page 44: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.
Page 45: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Here the membership label class_name:: tells the compiler that the function is the member of the specified class. The scope of the function is restricted to only the objects and other members of the class.

• To make the member function defined out side the class as an inline by adding inline as a prefix to the member function.

• Syntax• Return data type inline class_name :: function name(arguments)• { function body ;• }

Page 46: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• The following are the properties of a member function.• Several different classes can use the same function name; the membership label

will resolve their scope• A class can have multiple member functions with the same name as long as they

differ in terms of argument specification (data type or number of arguments) • Member functions can access the private data of the class, but a non-member

function cannot.• A member function can call another member function directly, without using a dot

operator.• There is no need to used member of operator (.) to access data members or

member function of a class within the class definition.

Page 47: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Objects:• Object can be defined as an Instance of a class. The process of creating objects

(variables) of the class is called class instantiation. In C++ the class variables are called as objects. The complier allocates required memory for objects. The size of an object is equal to the sum of the sizes of the individual data items of a class. The following is the general syntax of declaring objects.

• Type1: class class_name ob1,ob2,ob3........obn;• Type2:class_name obj1.obj2,obj3,…objn;• Where class_name is the name of the class for which we want to create objects. • ob1, ob2,.... obn are the objects of class name.

Page 48: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Accessing class members:-• To access members of a class using the member off operator dot ( . )• It is not possible to access private data members of a class out side the class

definition using dot operator• The member access operator is not required to access data members within the

class definition. • Syntax for accessing members of a class:• Access data members: object_name.datamember;• For Pointer Objects : Pointer-Object-Name -> member-name ; Access member functions: object_name.memberfunction(actual_parameters);• • The following is an example of creating an object of type ’item’ and invoking its

member function.• void main ( )• { item x; // creating an object of type item• x. getdata (20,20.5);• x put data ( );• }

Page 49: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Memory allocation for objects:-

• For each object of a class a separate copy of data members and one copy of member functions is created. For each object a separate memory is allocated and the address of that memory is stored in this pointer. By using this pointer the unique copy of member function of all the objects of a class are identified. This pointer is passed automatically to all the member functions of a class.

Page 50: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Example.docx

Page 51: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Nested Class :- – Nested Classes are the Classes defined inside a Class.– A class is defined within another class is called a nested class.– The Outer class is known as enclosing class.– Member function of a nested class have no special access to members

of an enclosing class.– Member function of an enclosing class have no special access to

members of a nested class.– Object of inner class can be created by using : : operator.

• Outer_class: : inner_class obj1;• The member function of the nested(inner ) class can be defined outside its

scope.• Return_type of fun outer_class: : inner_class: : fun() { }

Page 52: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• inner class.docx

Page 53: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Static data members :- • 1. Only a single copy of the Static data members are shared between Objects of

the Class.• 2. Static data members are initialized using the data type Class-Name and Scope-

Resolution Operator and data member-Name• 3. The Static members are declared using “ static “ keyword.• 4. The access rule of the data members of a class is same for the static data

member also.• 5. The Static Data Members should be created and initialized before the main

function control block • begins.• 6. Syntax :- • For variables :- static data-type variable-name ;•

Page 54: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

#include <iostream.h>#include<conio.h>

class sample{ static int a; // declaration public:

void incr( ){ a=a+1; }void putdata ( ){ cout <<"a="<<a<<endl; }

}; int sample::a; //by default initialize with 0

void main ( ){ sample x,y,z;

y. incr ();z.putdata( );

}

Page 55: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• staticmember.docx

Page 56: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Static member function:-• Like static member variables we can also have static member functions. A

member function that is declared as static has the following properties.• A static member function can have access to only other static

members(functions or variable) declared • in the same class.• A static member function can be called using the class name a follows.

– Class_name : : function_name ( )• A static member function also calls the class constructor explicitly.

• staticmemberandfunction.docx

Page 57: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

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 and member functions) 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.

• A static or non static member function cannot have the same name.

Page 58: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Friend Functions :- • 1. Friend functions are Not member functions of any class, but they

can access the private, protected and public members of the class.• 2. They are declared using “ friend “ keyword.• 3. They Don’t belong to any specific Class.• 4. They can be invoked as ordinary functions, with out using any

object.• 5. They can be defined outside the Class, here also they are defined

like ordinary functions and Not like member functions.– The access specifiers does not effect the visibility of Friend Functions.– Usually a friend function receives the object as arguments.

• 9. Syntax :- • friend return-type function-name ( input-arguments ) ;• 10. member function of one class can be defined as friend function of

another class.In such case they are• defined using scope resolution operator as shown below.

Page 59: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• class X• { ………• int fun1(); //member fun of X• };• class Y• { ………..• …………• friend int X: :fun1();• };• • friendfunction.docx

Page 60: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Constructor:

• 1. It’s a special function which has the same name as that of the Class name.

• It has No Return-Type, not even void• It should have public or protected access within the class and rarely

declared as private.• Constructor overloading is possible.• Constructors cannot be virtual.• It can not be static.• 4. Default arguments are possible.• 5. They are invoked automatically as soon as Objects of Class are created.• We can explicitly call Constructors of a Class.• it is possible to have more than one constructor in a class.• 7. They can Not be Inherited.

Page 61: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

– There are Three Basic Categories of Constructors.• a. Default Constructor: It takes no parameters and

performs no processing other than reservation of memory. It will always call by the compiler, if no user defined constructor is being provided.

• Syntax :- class-name ( ) { code } ;• b. Parameterized Constructor• Syntax :- Syntax :- class-name ( parameter-list ) { code } ;• c. Copy Constructor. • Syntax :- class-name ( reference of the same

class) { code } ;

Page 62: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

– If in any Class the Default Constructor is not defined then the compiler calls the implicit Default Constructor for Initializing objects of the Class. If there exists a Default Constructor defined in the Class then the defined Constructor will be called.

– If any of the three categories of Constructors are defined in the Class then the compiler in any case would never use the implicitly created Constructor, it would only use the user defined Constructor.

Page 63: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Default constructor:• class sample• { int a, b;• public:• sample()• { a=0;b=0;• }• void show()• { cout<<a<<b;• }• };• void main()• { sample s;• s.show();• }

Page 64: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Destructors :-

• It’s a special function which has the same name as that of the Class name.• 2. It has No Input-Arguments and No Return-Type, not even void.• 3. Its definition is preceded by Tild symbol “ ~ “.• 4. Its called implicitly as soon as the scope for any object finishes. Its

invoked for every object .• 5. The compiler creates a Destructor for every Class implicitly by default.• 6. If a Destructor is explicitly defined then the compiler will always use the

explicitly defined Destructor.• 7. Its generally used to free up the spaces allocated by using “ new “

operator during calling of Constructors. The de-allocation should be done using “ delete “ operator

• 8. The object that is created First is Destructed Last and object created Last is Destructed First.

• 9. Syntax :- ~class-name ( ) { code }

Page 65: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Example :- • Write a program to count the number of Objects created and destroyed

using constructor and destructor functions. The objects should be identified by a unique ID, which should be mentioned while creation and destruction of the object.

Page 66: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• class sample• {• static int count;• int a;• public:• sample() //Constructor• {• a=++count• cout<<"\n"<<" Object number = "<<a<<” is created”;• }• ~sample() //Destructor• {• cout<<"\nObject number = "<<a<<”is destroyed”;• }• };• int sample::count;• void main()• { clrscr();• {• sample ob1; //Created First Destructed Last• sample ob2; //Created Second Destructed Last Second• sample ob3; //Created Last Destructed First• } //As scope is finishing so Destructors would be called• getch();• }

Page 67: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Overloaded constructors (or) multiple constructors in a class

• It is possible to have more than one constructor function with in the same class by varying the argument list. This concept allows overloading the constructor function. The following is an example

• class sample• { int m,n;• public: sample ( ) // constructor -1• { m=n=0; }• sample (int i)// constructor - 2• { m=n=i; }• sample (int x, int y) // constructor - 3• { m=x; n=y: }• };• Thus the following declaration statements causes the appropriate constructors• sample i; // calls constructor-1• sample i(10); //calls constructor-2• sample i(10,20); // calls constructor -3

Page 68: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Constructors with default arguments:-

• It is possible to define constructors with default arguments. For ex. The constructor ‘complex’ can be defined as follows

• complex (float real, float imag=0);• • The default value of the argument imag is zero• Then the following statements, complex C(5.0); Assigns the value 5.0 to

the real variable and 0.0 to imag by default however the statement/ complex C(2.0,3.0); assigns 2.0 to real and 3.0 to imag. Here the actual parameter 3.0 overrides the default value (0.0)

Page 69: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Copy constructor• A copy constructor is a special constructor in the C++ programming

language used to create a new object as a copy of an existing object. This constructor takes a single argument: a reference to the object to be copied.

• Normally the compiler automatically creates a copy constructor for each class (known as an implicit copy constructor) but for special cases the programmer creates the copy constructor, known as an explicit copy constructor. In such cases, the compiler doesn't create one

• There are four instances when a copy constructor is called:• When an object is returned by value • When an object is passed (into a function) by value as an argument • When an object is constructed based on other object (pf same class) • When compiler generates a temporary object (as in 1 and 2 above; as in

explicit casting, etc...)

Page 70: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

• Constructors of a class can be invoked by using existing objects of the class.• Syntax :- obect . class-name :: constructor-function-call ;• #include <iostream.h>• #include<conio.h>• class code• { int d;• public:• code()• {cout<<"\ndefault constructor";• } // default constructor• code(int a) //constructor -2• { d=a;• cout<<"\nparameterized contrctr";• }• code(code &x)//copy constructor• { d=x.d;• cout<<"\ncopy cntrctr";• }• void display( )• { cout<<"\nd="<<d<<endl;• }• };

void main( ){ clrscr();

code A(100); //calls constructor - 2

code B(A);//calls copy constructorcode C=A;//calls copy constructorcode D; //calls

no argument constructorD=A;// It is assignment

not initialization hence no copy const. will be called

A.display ( );B.display ( );C.display ( );D.display ( ) ;getch();

}

Page 71: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Pointers in C++• A pointer is a variable which holds the address of any variable or a

function. A pointer can refer to an object of any one of the data type. • Pointer declaration:• Syntax : data type * variable_name; for example : int *x; or int* x; or int *

x;

Page 72: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Pointer operator:• Pointer operator can be represented as * (asterik) with a variable name. For

example if a variable of integer data type and also declared * with another variable, it means the variable is of type “pointer to integer”.

• int *ptr ;• float *fptr;• char *cptr;

Page 73: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Address operator• An address operator can be represented by the use of & (ampersand). The

operator immediately preceding a variable returns the address the of the variable.

• E,g int a;• Int *p;• p = &a ; or int *p = &a; // address of variable a is assigned to pointer

variable p.• & operator can be used only with a simple variable or an array element .• E.g &120 ; //invalid• int x[10];• &x; //illegal use of ampersand operator.• &x[0] or &x[i] //valid;

Page 74: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Constant pointer• C++ adds the concept of constant pointer and a pointer to a constant.• char * const ptr1=”GOOD”; // constant pointer• we can not modify the address that ptr1 is initialized to.• int const * ptr2 = &m; // pointer to a constant• ptr2 is declared as pointer to constant> It can point to any variable of

correct type but the contents of what • it points to cannot cannot be changed.• We can also declare both the pointer and the variable as constants in the

following way:• Const char * const ptr = “xyz”;• This statement declares ptr as a constant pointer to the string which has

been declared a constant. In this case neither the address assigned to the pointer ptr nor the contents it points to can be changed.

Page 75: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Void pointer• Syntax: void *variable for example: void *x;• The void type of pointer is a special type of pointer; void pointers are

pointers that point to a value that has no type. This allows void pointers to point to any data type, from an integer value or a float to a string of characters.

• int .a;• char c;• void *p;• p = &a;• cout<<” Value of a=” , *((int *)p); //void pointer is typecasted to

int type.

Page 76: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Null pointer• A null pointer is a regular pointer of any pointer type which has a special

value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.

• Syntax: data type * variable = NULL or data type *variable = 0

• For example int *x = 0 or int *x = NULL:

Page 77: Programming paradigms Programming paradigms means the way of writing a program depending upon the requirements. These requirements mainly focus the task.

Const class objects and member functions

• we covered the merits of passing function parameters as const variables. To recap, making variables const ensures their values are not accidentally changed. This is particularly important when passing variables by reference, as callers generally will not expect the values they pass to a function to be changed.

• Just like the built-in data types (int, double, char, etc…), class objects can be made const by using the const keyword. All const variables must be initialized at time of creation. In the case of built-in data types, initilization is done through explicit or implicit assignment:

• const int nValue = 5; // initialize explicitly• const int nValue2(7); // initialize implictly• In the case of classes, this initialization is done via constructors:• const Date cDate; // initialize using default constructor• const Date cDate2(10, 16, 2020); // initialize using parameterized constructor