Sei sulla pagina 1di 29

mastery......................................................3 modified....................................................

Object....................................................1, 5 overloading..........................................5, 18

Beginning with C++...................................................................................................................1 CHAPTER-1Fundamental Data Types......................................................................................1 Getting acquainted with C++.....................................................................................................5 Program features........................................................................................................................5 Function.....................................................................................................................................5 Fundamental Data Types...........................................................................................................6 Data Type Number of bytes Used for representing..................................................................6 User-defined Data Types...........................................................................................................7 Variables....................................................................................................................................7 Declaration of variables.............................................................................................................7 Operators....................................................................................................................................7 Arithmetic Operators.................................................................................................................7 Relational Operators..................................................................................................................7 Logical Operators.......................................................................................................................7 Unary Operators.........................................................................................................................8 Assignment Operators................................................................................................................8 Programming constructs............................................................................................................8 Conditional constructs ............................................................................................................8 If statement.................................................................................................................................8 Break keyword...................................................................................................................9 Default keyword.................................................................................................................9 Loop Constructs.........................................................................................................................9 Introduction................................................................................................................................9 While loop..................................................................................................................................9 Do-while loop............................................................................................................................9 For loop....................................................................................................................................10 Break statement........................................................................................................................10 Continue statement...................................................................................................................11 CHAPTER-2............................................................................................................................12 Arrays and Pointers..............................................................................................................12 CHAPTER-3............................................................................................................................16 Functions and Structures......................................................................................................16 CHAPTER-4............................................................................................................................24 Classes and Objects..............................................................................................................24
Beginning with C++ CHAPTER-1Fundamental Data Types Object Orientation and Object Oriented Programming languages are playing an increasingly important role in the computing industry. With the gradual decline in hardware costs, the cost of computing systems is largely dominated software. More powerful tools, operating systems, and programming languages are evolving to keep up with the pace of hardware development.

Software for different applications need to be developed under these environments, which is a complex process.

As a result, the relative cost of the hardware of a computing system. Software maintenance is the process of modifying or extending the capabilities of the existing software. It requires mastery over the understanding and modifying the existing software, and finally revalidating the modified software.

Introduction to C++ C++ , originally known as C with classes. C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories in the early eighties. Two languages contributed to its design-C, which provided low-level features and Simula 67, which provided the class concept. C++ is a combination of the object oriented features of a language called Simula 67 and the power and elegance of C. Therefore, C++ is an extension of C with a major addition of the class construct features of Simula 67. Other object-oriented languages include Smalltalk and Ada. Some of the most important features that C++ adds on to C are classes, function overloading, and operator overloading. These features enable developers to create abstract classes and support polymorphism. Getting acquainted with C++ Object Oriented Programming is a new way of solving problems with computers: instead of trying to mould the problem into something familiar to the computer, the computer is adapted to the problem. Object Oriented Programming is designed around the data being operated upon as opposed to the operations themselves. Instead of making certain types of data fit into specific and rigid computer operations, these operations are designed to fit to the data. This is as it should be, because the sole purpose of a computer program is to manipulate data. C++ is a versatile language for handling very large programs. It is suitable for virtually any programming task including development of editors, compilers, databases, communication systems and any complex real-life application systems. Program features Like C, the C++ program is a collection of functions. The program contains only one main() function, which is where execution starts. Every statement ends with a semicolon, C++ is a case sensitive language. #include<iostream.h> // Example1.cpp void main() { cout<<My First Program in C++; } Comments Comments are used for better understanding of the program statements. The comment entries start with // symbol and terminate at the end of one line. In the program second line is the example of comment. #include directive The include directive instructs the compiler to include the contents of the file enclosed with angular (<>) brackets into the program. In the above program the file, iostream.h is included in example1.cpp. Function All C++ programs comprise of one or more functions. The function is identified by a function name and its body. The function name is identified by a word followed by parenthesis. In the above program main() is a function name. The execution of a program starts with main function. The keyword void along with the function name signifies that function does not return a value.

The Output Operator (<<): The only statement in the above program is the output statement. The statement cout<<My First Program in C++; causes the string My First Program in C++ to be displayed on the screen. This statement introduces two features specific to C++, cout and <<. The identifier cout is a predefined object that represents the standard output stream in C++. A stream is an abstraction that refers to a flow of data. The operator << is called the insertion or put to operator. The insertion operator inserts the contents of the variable on its right to the object on its left. The iostream.h file: #include<iostream.h> causes the contents of the file iostream.h to be added to the program in which it is included. It contains declaration for the identifier, cout and the operator <<. This header is necessary to include at the beginning of all programs that uses input or output statements. Input using cin: Like cout, the identifier cin is a predefined object that corresponds to the standard input stream which in this case represents the keyword. The following program illustrates the usage of cin: #include<iostream.h> //Example2.cpp void main() { int a; cout<<Enter a number; cin>>a; cout<<The number entered is <<a; } The extraction operator (>>) works similar to the %s used with the scanf() in C. When the program prompts for input, try entering it will ask a number prompting Enter a number. After inputting a number it displays it on the screen with the message, The number entered is number. Data Types The data types in C++ can be classified under various categories, that are fundamental and user-defined. Fundamental Data Types The fundamental data types are those which are at the lowest level, i.e., those that are used for actual representation in memory. The fundamental data types and their function are listed below: Data Type Int Float Char Bool Number of bytes 4 4 1 1 Used for representing Integers Floating point numbers Characters and strings True or False values

The size of a data type can be determined by using the sizeof operator. The following statement: sizeof(datatype/object); determines the size of a particular type or object. User-defined Data Types User-defined data types are those which are defined by the user using the typedef keyword. These can also be created in two other ways: struct class Variables Variables are fundamental to any language, values can be assigned to variables which can change throughout the execution of program. The value assigned to a variable is placed in the memory allocated to that variable. These can be created using the keyword int, char, float, etc. Declaration of variables A declaration is a statement that introduces a variable into the program. It specifies a type for that variable. Here is difference between C and C++ that C requires all variables to be declared at the beginning of the program or function but in C++ variables can be declared anywhere within the program or function as and when they are required. Operators A variable, when declared of a particular type, determines the set of legal operations that can be performed on it. The different sets of operators are given below: Arithmetic operators Unary operators Relational operators Assignment operators Logical operators

Arithmetic Operators These are + (add), - (subtract), * (multiply), / (divide) and the % (modulus). Relational Operators These are < (less than), > (greater than), == (equal), != (not equal), <= (less than equal), >= (greater than equal). These operators compare two values and return a logical value. Logical Operators These are && (and), || (or), ! (not). These operators evaluate two expressions and evaluation stops as soon as the truth or falsehood of the result is known.

Unary Operators The unary operators operate on one operand. C++ provides ++ (increment) and -(decrement). The increment operator adds 1 to its operand and decrement operator subtracts 1 from its operand. Assignment Operators Expressions such as a=a+1, in which the variable on the left is immediately repeated on the right can be written in a compressed form as a+=1 or a++. The operator += is called the assignment operator. Programming constructs Conditional constructs If statement We can perform execution of a statement or a set of instructions based on a given condition. Following example illustrates its use: #include<iostream.h> //Example3.cpp void main() { int a,b; cout<<Enter any two numbers; cin>>a>>b; if(a>b) { cout<<The bigger number is <<a; else cout<<The bigger number is <<b; } } Above code can also be written using the conditional ternary operator as follows: (a>b) ? cout<<The bigger number is<<a : cout<<The bigger number is<<b; switch-case statement C++ has a built-in multiple branch selection statement called switch. This statement successfully checks the value of an expression against a list of integer or character constants. When match is found the statements associated with that constant are executed. #include<iostream.h> //Example4.cpp void main() { int a,b,ch,sum,sub; cout<<1- For addition<<\n; cout<<2- For subtraction<<\n; cout<<Enter your choice; cin>>ch; switch(ch) { case 1:

cout<<The first number<<\n; cin>>a; cout<<The second number<<\n; cin>>b; sum=a+b; cout<<The addition is <<sum<<\n; break; case 2: cout<<The first number<<\n; cin>>a; cout<<The second number<<\n; cin>>b; sub=a-b; cout<<The subtraction is <<sub<<\n; break; default: cout<<Wrong selection! try again!<<\n; } } Break keyword The break statement in the above example causes an exit from the switch body. Control goes to the first statement following the end of switch construct. In above program if the break statement is not used the control passes to the statement for the next case and the remaining statements in the switch construct are executed. Default keyword It gives the switch construct a way to take action if the switch variable does not match any of the conditions specified in the switch construct. Loop Constructs Introduction A loop causes a section of program to be repeated a certain number of times. When the condition becomes false the looping ends and control is passed to the statement following the loop else repetition continues as long as the condition is true. While loop The while construct repeats the body of the loop as long as the loop condition is true. The following program illustrates this #include<iostream.h> //Example5.cpp void main() { int i=0; while(i<=10) { cout<<i; i++; } } Do-while loop In a while loop the test expression is evaluated at the beginning. If the test expression is

false the loop body is not executed at all. If the loop body must be executed at least once then the do-while construct should be used. It places the test expression at the end of the loop. The following program illustrates this #include<iostream.h> //Example6.cpp void main() { int i=0; do { cout<<i; i++; }while(i<=10); } For loop The while and do-while loops are used when the number of iterations is not known. When the number of iterations is known a for loop is used. For Example #include<iostream.h> //Example7.cpp void main() { int i; for(i=0;i<=10;i++) { cout<<i; } } Break statement The break statement is used to terminate a case in a switch construct as well as in for loop. When break is encountered inside a loop, the loop is terminated and control passes to the statement following the loop body.

#include<iostream.h> //Example8.cpp void main() { int n,sum=0; do { cout<<Enter a number, exit by 0; cin>>n; if(n>10) { cout<<Number should be less than 10; break; } sum=sum+n; }while(n!=0); cout<<The sum is <<sum; } Continue statement This statement forces the next repetition of the loop to take place skipping any code following the continue statement in the loop body. #include<iostream.h> //Example9.cpp void main() { int n,sum=0; do { cout<<Enter a number, exit by 0; cin>>n; if(n>10) { cout<<Number should be less than 10; continue; } sum=sum+n; }while(n!=0); cout<<The sum is <<sum; }

CHAPTER-2 Arrays and Pointers Introduction to Arrays An array is a collection of elements of the same type that are referenced by a common name. Each element of an array can be referred to by an array name and a subscript or an index. Declaring one dimensional arrays type varname[size]; The following program illustrates the use of array: #include<iostream.h> //Example9.cpp void main() { int n[5],i; for(i=0;i<=5;i++) { cout<<Enter the number <<i; cin>>n[i]; } for(i=0;i<=5;i++) { cout<<The number <<i<< is; cout<<n[i]; } } Double dimensional array A 2D array is a grid containing rows and columns where each element can be accessed by a specifying row and column coordinates. Declaring double dimensional arrays type varname[size1][size2]; where size1 refers to row and size2 refers to column. The following program illustrates the use of 2D array: #include<iostream.h> //Example10.cpp void main() { int n[3][2],i,j; for(i=0;i<3;i++) { for(j=0;j<2;j++) { cout<<Enter the number for <<i<< row and <<j<< column<<endl; cin>>n[i][j];

} } for(i=0;i<3;i++) { for(j=0;j<2;j++) { cout<<The number are <<n[i][j]; } cout<<\n; } } Pointers A pointer is a variable which holds an address. Every variable declared in a program has two components 1- Address of the variable 2- Value stored in a variable C++ has two unary operators referring to the components of a variable, the first operator being &, known as address operator and returns the address of the variable. The second operator *, known as indirection operator and returns the value stored at any address in memory. Declaration of Pointers The variable name should be preceded by an asterisk (*). The data type of the pointer should be the same as the data type of the variable to which it will point. E.g., int *a; Declares a pointer called a which points to any integer value. Initialisation of Pointers It is always a good practice to initialise pointers as soon as they are declared. Initialising a pointer involves the assigning of the address of a variable to the pointer. E.g., int *x,a; x=&a; float *y,b; y=&b; b=2.3; Here x and y are a pointer to int and float values respectively. Pointer manipulation Complicated manipulation of pointers is possible through the use of the address and dereferencing operators and also by combining them with relational operators (+ and -). Following example illustrates its application: #include<iostream.h> //Example11.cpp void main() {

int x=10,y=20,temp; int *a, *b; a=&x; b=&y; cout<<Before swapping the value of a and b are<<endl; cout<<a<<b; temp = *a; *a = *b; *b = temp; cout<<After swapping the value of a and b are<<endl; cout<<a<<b; } Arrays in Pointers A pointer has also been defined as a variable that holds the address of another variable. Therefore the conclusion that follows is that the name of an array is actually a pointer. Example #include<iostream.h> //Example12.cpp void main() { char str1[]=Welcome to SMTCI; char *str2; cout<<str1; str2=str1; cout<<str2; } In above program str1 is a string variable and str2 is a pointer to character. It displays the value of str1 and str2 after copying address of str1 to str2. Dynamic memory allocation using new and delete operators new operator Pointers provide the necessary support for dynamic memory allocation system. Dynamic memory allocation means a method by which variable is placed in memory at the time of execution. It saves memory space in life cycle of a software. syntax <var>=new <type> The type of variable mentioned on the left hand side and the type mentioned on the right hand side should match. Consider the following example: int *a; a=new int;

char *s; s=new char; char *s; s=new char [10]; #include<iostream.h> //Example13.cpp void main() { int *a; a=new int; cin>>a; cout<<a; } delete operator The delete operator is used to release a variable from memory so that we can make memory free after usage of a variable in the program. syntax delete <var> where var is a pointer variable. Above example may be modified in the following manner: #include<iostream.h> //Example14.cpp void main() { int *a; a=new int; cin>>a; cout<<a; delete a; } The variable a is released from memory after its expiry.

CHAPTER-3 Functions and Structures Introduction to Functions A function is a single comprehensive unit that performs a specified task repeatedly each time a function is called at various level of the program. Why does a function required When programs become large, a single list of instructions becomes difficult to understand for this reason functions are adopted. A function has a clearly defined purpose and clearly defined interface to other functions in the program. Another reason for using function is to reduce the program size. The main function may call other functions which in turn may again call still other functions. When a function is called the control is transferred to the first statement in the called function. The statement following the function call, or the expression being evaluated with the function, is put into a stack (dynamic list created while execution of a program). After the function is executed, control returns to the calling function, and execution continues with the evaluation of the expression (calling from the stack) in which the call was made. A value can be returned when a function completes the specified task and that value can be used as an operand in the expression. Functions can communicate data between themselves in two ways: 1- Through global variables 2- Through an argument or parameter list Functions can be classified into two types, they are: 1- Library 2- User-defined Library functions are a set of predefined functions like printf(), scanf(), strlen(), etc. The main distinction between the library and user-defined functions is that library functions are not required to be written by the user whereas a user-defined function has to be written by the users at the time of writing a program. However a user-defined function can later become a part of C++ library. Function prototyping Like any other variable it is necessary to declare or prototype a function to inform the compiler that the function would be referenced at a later point in the program. E.g., int sum(); or int sum(int x,int y); Defining a function with argument Function definition contains the code for the function. The function definition for sum() is given below:

#include<iostream.h> //Example16.cpp void main() { int a,b,s; int sum(int,int); cout<<Enter any two numbers<<endl; cin>>a>>b; s=sum(a,b); cout<<s; } int sum(int a,int b) { return a+b; } Calling functions Functions can be called in the following ways: 1- Call by value 2- Call by reference Call by value In this method, function passes a list of arguments to the function definition copying its values to the formal parameters. The following illustrates this use: #include<iostream.h> //Example17.cpp void main() { int x=10,y=20; swap(int,int); cout<<Before swapping the value of x and y are<<endl; cout<<x<<y; swap(x,y); } swap(int a,int b) { int temp; temp = a; a = b; b = temp; cout<<After swapping the value of a and b are<<endl; cout<<a<<b; } Call by reference In this method, in passing reference arguments, a reference to the variable in the calling program is passed. It is useful when the function does need to modify the values of the original variables in the calling program. The following illustrates this use:

#include<iostream.h> //Example18.cpp void main() { int x=10,y=20; swap(int *,int *); cout<<Before swapping the value of x and y are<<endl; cout<<x<<y; swap(&x,&y); cout<<After swapping the value of x and y are<<endl; cout<<x<<y; } swap(int *a,int *b) { int temp; temp = *a; *a = *b; *b = temp; } Passing arrays to functions using pointer arithmetic Arrays are inherently passed to functions by call by reference method. The following program illustrates this: #include<iostream.h> //Example19.cpp void main() { int x[5]={1,2,3,4,5}; arrfun(int *); arrfun(x); } arrfun(int *a) { for(int i=0;i<5;i++) { cout<<*a; a++; } } Above program passes address of first byte of element stored in array to the function. a++ depicts increment in pointer (address) and *a sends the value at address. Function overloading Function overloading is a process of using the same name for two or more functions. We can redefine a function using number of argument list and its type. The number, type, or

sequence of parameters for a function is called the function signature. The following program illustrates this: #include<iostream.h> //Example20.cpp void main() { int a=3,b=5; float x,y; int add(int,int); float add(float,float); cout<<Enter the values <<endl; cin>>x>>y; cout<<add(a,b); cout<<add(x,y); } int add(int a,int b) { return a+b; } float add(float x,float y) { return x+y; } Structures We can create and use data types other than the fundamental data types. These data types are called user-defined data types. We can create user-defined data type using two ways: 1- Structures 2- Classes Data type defined using the keyword struct is called structure. Structure is a collection of different type of variables that are referenced by a single name. Structure definition forms a template that is used to create structure variables. The variables that make up the structure are called structure elements. syntax struct <tagname> { data type1; data type2; data type3; .... }; Notice that the declaration is terminated by a semicolon. This is because a structure declaration is a statement. The compiler automatically allocates sufficient memory to accommodate all the elements that makes up the structure.

dot operator Individual structure element can be referenced by combining the dot operator and the name of the structure variable. syntax <structure var>.<element name>... The following example illustrates the use of structure: #include<iostream.h> //Example21.cpp struct emp { int id; char name[20]; float sal; }; void main() { cout<<Enter the Emp id <<endl; cin>>emp.id; cout<<Enter the Emp name <<endl; cin>>emp.name; cout<<Enter the Emp salary <<endl; cin>>emp.sal; cout<<The Emp id is <<emp.id<<endl; cout<<The Emp name is <<emp.name<<endl; cout<<The Emp salary is <<emp.sal<<endl; } The -> (arrow) operator The -> operator is used when structure element has to be accessed through a pointer. The pointer should be initialised before it can access any structure element. Following code fragment illustrates this: student *stud; stud = new student; stud -> stdnum=123; stud -> studage=23; .... .... In above fragment student is a structure name while stud is a pointer to structure. Data storage types There are three types of storage types as given below: 1- auto 2- static 3- extern auto

Variables defined in a function are in memory and retain their value only as long as the function is in execution. E.g., int a,age,rollno; char name[25]; are exactly treated as auto: auto int a,age,rollno; auto char name[25]; The word auto may be used in declarations for clarity. The following example illustrates this: #include<iostream.h> //Example22.cpp void main() { void sum(void); for(int i=0;i<5;i++) { sum(); } } void sum(void) { int x=1; cout<<x<<endl; x++; } static Static type data retains its value even after the function to which it belongs has been executed. E.g., #include<iostream.h> //Example23.cpp //Program for showing difference between auto and static types void main() { void sum(void); for(int i=0;i<5;i++) { sum(); } } void sum(void) { static int x=1; cout<<x<<endl; x++; } Program22.cpp will display the value of x as 1 for 5 times while Program23.cpp will generate 5 different values of x.

extern A variable can also be declared in a manner such that it is available to a function in a program file that is, it is a global variable. The following example illustrates this: #include<iostream.h> //Example24.cpp int a=1; void main() { void sum(void); for(int i=0;i<10;i++) { sum(); a++; } } void sum(void) { cout<<a<<endl; } The extern data type does not allocate any memory for a variable but merely declares it to have been created elsewhere in a program. This declaration must be used to access a global variable declared in another program file. The following program illustrates this: #include<iostream.h> //Example25.cpp int a=1; void main() { void disp(void); for(int i=0;i<10;i++) { disp(); a++; } }

//Example26.cpp void disp(void) { extern int a; cout<<a<<endl; } Example25.cpp declares a global variable called a. Then it calls a function disp which is written in another program file called example26.cpp. Example26.cpp refers to the variable a using the extern declaration since the a is declared in the program example25.cpp and not in program example26.cpp.

CHAPTER-4 Classes and Objects Introduction to OOPS The object oriented paradigm is built on the foundation laid by the structured programming concepts and data abstraction. Data abstraction does for data what functional abstraction does for operations. OOP facilitates creating reusable codes that can eventually save a lot of work. A feature called polymorphism permits to create multiple definitions for operators and functions. Another feature called inheritance permits to derive new classes from old classes. Characteristics of OO language The major elements of object oriented languages are given below: 1234567Classes Objects Data Abstraction Encapsulation Inheritance Polymorphism Reusability

Classes We are familiar to primitive data types like, int, float, char, etc. The programmer may define its format and behaviour by defining a class. E.g., date is user-defined data-type and computer does not know about dates. Programmer may define the behaviour of dates by designing a date class. This class will express the format of date and the operations that can be performed on it. A class serves as a blue print or a plan or a template. It specifies what data and what function will be included in objects of that class. Objects An object oriented programming the problem is divided into objects. Thinking in terms of objects rather than functions makes the designing a program easier. Following may be few factors that can be treated as objects in different situations: 12345GUI elements like windows, menus, icons, etc. Data structures like linked list, stacks and queues, etc. Hardware devices like disk drive, printer, etc. Customers, sales persons in a sales tracking system Computers in a network model, etc.

Data Abstraction With data abstraction, data structures can be used without having to be concerned about the exact details of implementation. E.g., floating point numbers are abstracted in programming language. We are not required to know how a floating point number is represented in binary while assigning value to it. It is only recently language have been developed to define your own abstract data types. Encapsulation It is a process for data hiding from accidental alteration into a single entity. For doing this we can declare the members of class private. In this way we can hide and show unnecessary information defining private and public members. Inheritance OOP permits us to create own data types (classes) just like the types built into the language. The user-defined classes can use other classes as building blocks. New classes an be built from the old ones using inheritance. The new class is called derived class can inherit the data structure and functions of the original or the base class. The new class can add its own attributes and functions to those it inherits from its base class. Polymorphism A single function name can be used for the same operation performed on related derived classes even if the implementation of that function varies from class to class. Reusability Once a class is completed and tested it can be distributed to other programmers for use in their own programs. It is known as reusability. Those programmers can add new features or change that existing classes deriving new classes from old ones. The tried and tested capabilities of base classes do not need to be redeveloped. Programmers can save time writing new code instead of wasting time in rewriting the existing code. Class declaration A class declaration is similar to structures in C++. Syntax class <name> { data type1, data type2, data type3... function1(), function2(),function3()... }; E.g., #include<iostream.h> //example27.cpp class values { int a; float b; public: void input() { a=5; b=5.5; } void display() { cout<<The value of a is <<a<<endl;

cout<<The value of b is <<b<<endl; } }; void main() { values obj; obj.input(); obj.display(); } The above program will produce the following output: The value of a is 5 The value of b is 5.5 In above program a is an int while b is a float member. Input() and display() are the two member functions which will input and show the values respectively. obj is one of the object of values class. Note that both the function members are declared in public part while data members are in private. If no scope specifier is given members will be treated as private. Only public members are accessible outside the class through object of the declared class, obj in this case. The relation of object and class shows client server model. The scope resolution operator (::) The function defined inside a class is treated as inline function by default. So using this, a programmer can define a lengthy member function outside the class definition, without the function losing its connection with the class itself. E.g., #include<iostream.h> //example28.cpp class values { int a; float b; public: void input(); void display(); }; void values::input() { a=5; b=5.5; } void values::display() { cout<<The value of a is <<a<<endl; cout<<The value of b is <<b<<endl; } void main() { values obj; obj.input();

obj.display(); } Constructors It is a special function member having the same name as of the class which is autoexecutable as object is created into memory. It assigns a default value to the data members or instances. E.g., #include<iostream.h> //example29.cpp class values { int a; float b; public: void values(); void display(); }; void values::values() { a=5; b=5.5; } void values::display() { cout<<The value of a is <<a<<endl; cout<<The value of b is <<b<<endl; } void main() { values obj; obj.display(); } In the above example function values() is invoked automatically as object is created. Constructors are an exception to a general rule in C++, that, all function must specify a return type and also return a value of the specified type. Since it is to be invoked automatically so it is declared in public part of class. A constructor can also take parameters. A constructor that does not take any parameter is called default constructor. Above program is the example of default constructor. Let us discuss a constructor with parameters. #include<iostream.h> //example30.cpp class values { int a; float b; public: void values(int,float); void display(); }; //Constructor having parameters

void values::values(int x,float y) { a=x; b=y; } void values::display() { cout<<The value of a is <<a<<endl; cout<<The value of b is <<b<<endl; } void main() { values obj(5,5.5); obj.display(); } Destructors These are complementary to constructors. A destructor may be called either when the object goes out of scope or when it is destroyed explicitly using the delete operator. A destructor, like a constructor has the same name as the class preceded by ~ (tilde) symbol. A class can not have more than one destructor and a destructor can not have arguments or specify a return value. E.g., #include<iostream.h> //example31.cpp class values { int a; float b; public: void values(int,float); void display(); ~values(); }; void values::values(int x,float y) { a=x; b=y; } values::~values() { cout<<This is destructor; a=0; b=0; } void values::display() { cout<<The value of a is <<a<<endl; cout<<The value of b is <<b<<endl; } void main() { values obj(5,5.5);

obj.display(); } To explicitly call a destructor for an object the following syntax is used: <object>.<classname>::<destructor>; copy constructor There may arise a situation where class constructors are not invoked to initialise a newly defined class object.