C Internship
C Internship
Semester II
Advisor: Prof. A. D. Gangarde (Chairman, Bord of Studies Comp. Sc.) Chairman: Prof. S. S. Deshmukh (Vice-Principal & Head, Modern College, Shivajinagar, Pune 5.) Co-ordinator: Mr. A. V. Sathe (Modern College, Shivajinagar, Pune 5) Authors: Mrs. Manisha Suryavanshi (Modern College, Shivajinagar, Pune 5) Mrs. Madhuri Ghanekar (Modern College, Shivajinagar, Pune 5) Mr. S. G. Lakhdive (Prof. Ramkrishna More College, Akurdi, Pune) Mr. Parag Tamhankar (Abasaheb Garware College, Pune.) Mrs. Manisha Jagdale ( Annasaheb Magar College, Hadapsar, Pune) Board of Study (Computer Science) members: 1. Mr. M. N. Shelar 2. Mr. S. N. Shinde 3. Mr. U. S. Surve 4. Mr. V. R. Wani 5. Mr. Prashant Mule 6. Dr.Vilas Kharat 7. Mrs. Chitra Nagarkar
Table of Contents About the work book4 1. Introduction to C++ Programming 6 2. Functions in C++..21 3.Classes and Objects..27 4.Constructors and Destructors.41 5.Operator Overloading...48 6.Inheritance...58 7. Formatted I/O.66 8.Exception Handling78 9.File I/O..84 10.Templates89
5) Students will be assessed for each exercise on a scale of 5 1. Note Done 2. Incomplete 3.Late Complete 4.Needs Improvement 5.Complete 6.Well Done 0 1 2 3 4 5
Instructions to the Practical In-charge 1) Explain the assignment and related concepts in around ten minutes using white board if required or by demonstrating the software. 2) Choose appropriate problems to be solved by student. 3) After a student completes a specific set, the instructor has to verify the outputs and sign in the provided space after the activity. 4) Ensure that the students use good programming practices. 5) You should evaluate each assignment carried out by a student on a scale of 5 as specified above ticking appropriate box. 6) The value should also be entered on assignment completion page of respected lab course.
Reading
You should read following topics before starting this exercise: 1. Difference between C and C++. 2. Features of C++ 3. Concept of Translators, Compilers 4. C++ functions
Ready References
Data Types with Modifiers in Combination Type unsigned char signed char unsigned int signed int short int unsigned short int signed short int long int unsigned long int signed long int long double Bit Width 8 8 32 32 16 16 16 32 32 32 80 0 to 255 -128 to +127 0 to 4,294,967,295 -2,147,483,648 to 2,147,483,647 -32,768 to 32,767 0 to 65,535 -32,768 to 32,767 same as int same as unsigned int same as signed int 3.4E-4932 to 1.1E+4932 Range
Operator Group 4 ->* .* Group 5 * / % Group 6 + Group 7 << >> Group 8 < <= > >= Group 9 == eq != not_eq Group 10 & bitand Group 11 ^ xor
Description Member pointer ptr->*var = 24; selector Member object selector obj.*var = 24; Multiplication Division Modulus Addition Subtraction Bitwise shift left Bitwise shift right Comparison less-than Comparison less-thanor-equal-to Comparison greaterthan Comparison greaterthan-or-equal-to
Example
Overloadable YES NO YES YES YES YES YES YES YES YES YES YES YES YES YES
int i = 2 * 4; float f = 10.0 / 3.0; int rem = 4 % 3; int i = 2 + 3; int i = 5 - 1; int flags = 33 << 1; int flags = 33 >> 1; if( i < 42 ) if( i <= 42 ) ... if( i > 42 ) if( i >= 42 ) ...
Comparison equal-to if( i == 42 ) ... Alternate spelling for == Comparison not-equalif( i != 42 ) to Alternate spelling for != Bitwise AND flags = flags & 42; Alternate spelling for & Bitwise exclusive OR flags = flags ^ 42; (XOR) Alternate spelling for ^
YES
YES
Group 12 | Bitwise inclusive flags = flags | 42; (normal) OR Alternate spelling for | YES
bitor Group 13 && Logical AND if( conditionA && conditionB ) and Alternate spelling for && Group 14 || Logical OR if( conditionA || conditionB ) ... or Alternate spelling for || Group 15 (right-to-left associativity) Ternary conditional ?: int i = (a > b) ? a : b; (if-then-else) Group 16 (right-to-left associativity) Assignment = int a = b; operator Increment and += a += 3; assign Decrement and -= b -= 4; assign *= Multiply and assign a *= 5; /= Divide and assign a /= 2; %= Modulo and assign a %= 3; Bitwise AND and &= flags &= new_flags; assign and_eq Alternate spelling for &= Bitwise exclusive or ^= flags ^= new_flags; (XOR) and assign xor_eq Alternate spelling for ^= Bitwise normal OR |= flags |= new_flags; and assign or_eq Alternate spelling for |= Bitwise shift left and <<= flags <<= 2; assign Bitwise shift right >>= flags >>= 2; and assign Group 17 throw throw exception throw EClass(Message); Group 18 Sequential for( i = 0, j = 0; i < 10; i++, j++ ) , evaluation operator
YES
YES
NO
YES
YES
10
1. The statement #include <iostream> is a preprocessor directive that includes the necessary definitions so that a program can do input and output using the iostream library. 2. The statement using namespace std 3. The main() function can have zero or more parameters. The function may have return type as int or void. All C++ programs must have exactly one main function. Program execution always begins from main. 4. The brace marks the beginning of the body of main. 5. The line defines an integer variable called as workdays. 6. The line defines three float variables workHours, payRate, weeklyPay 7. The line is an assignment statement. It assigns value 5 to the variable workDays. 8. The line assigns 7.5 to the variable workHours. 9. The line assigns 38.55 to the variable payRate. 10. The line calculates weeklyPay as a product of workdays, workHours and payRate. 10-13. The symbol << is an output operator which takes an output stream as its left operand and an expression as its right operand. These lines will produce following output: Weekly Pay = 1445.625 14. This brace marks the end of the body of main. Compiling a C++ Program 1 2 3 4 $ g++ pay.cpp $ ./a.out Weekly Pay = 1445.625 $
11
C++ Program
Object Code
ExecutLINKER able
Example 2: A simple C++ program to compute average velocity of car. 1 // Program: Compute average velocity of car 2 #include <iostream> 3 using namespace std; 4 int main() 5 { 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cout << "Start Milepost?"; int StartMilePost; cin >> StartMilePost; cout << "End Milepost?"; int EndMilePost; cin >> EndMilePost; cout << "End time (hours, minutes, seconds)?"; int EndHour, EndMinute, EndSecond; cin >> EndHour>>EndMinute>>EndSecond; float ElapsedTime = EndHour + (EndMinute/60.0)+(EndSecond / 3600.0); int Distance = EndMilePost - StartMilePost; float Velocity = Distance / ElapsedTime; cout << "\nCar traveled " << Distance << " miles in "; cout << EndHour << " hrs " << EndMinute << " min " << EndSecond << " sec\n";
21 cout << "Average velocity was " << Velocity << " mph" << endl; 22 return 0; 23 }
12
1 2 3 4 5
6 End time (hours, minutes, seconds)?12 20 30 7 Car traveled 900 miles in 12 hrs 20 min 30 sec 8 $
Result types for binary floating point operations Type of left operand float double long double float double long double Type of right operand float double long double double double long double long double long double long double
13
Result types for mixed mode arithmetic operations Type of left operand int int long float double long double int long float double long double long long long float double Type of right operand float float float float double double double double double double long double long double long double long double long double
1.6 Casts
It is possible to force an expression to be of a specific type by using a construct called cast. The general form of this cast is (type)expression For example, if you want to make sure the expression (x/2) is evaluated to type float, you can write (float) x / 2 Example 3: Use of cast in C++. 1 2 3 4 5 6 7 8 9 #include <iostream> using namespace std; int main() { int i; for(i=1;i<100;i++) cout << i << return 0; }
NOTE:
C++ defines five casting operators. The first is the traditional style cast inherited from C. The remaining four casting operators are dynamic_cast, const_cast,
reinterpret_cast, and static_cast. Another feature of C++ is Run- Time Type Identification (RTTI).
14
10 int sqr_it(int x) 11 { 12 x = x*x; 13 return x; 14 } What will be the value of t after execution of sqr_it() function?Does the value of t modified after the execution of sqr_it() function? (Execute above program and analyze the output.) Answer:
15
8 9 10 11 12 13 14 15 16
i = 10; j = 20; cout << "Initial cout << i << ' ' swap(&j, &i); // cout << "Swapped cout << i << ' ' return 0; }
values of i and j: "; << j << '\n'; call swap() with addresses of i and j values of i and j: "; << j << '\n';
17 // Exchange arguments. 18 19 20 21 22 23 24 void swap(int *x, int *y) { int temp; temp = *x; // save the value at address x *x = *y; // put y into x *y = temp; // put x into y } What will be the values of j and i after execution of swap() function? Does the values of j and i are modified after execution of swap() function? (Execute above program and analyze the output.) Answer:
16
11 12 13 14 15 16 } }
return 0;
What will be the value of variable val after execution of function f()? (Execute above program and analyze the output.) Answer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream> using namespace std; // Declare swap() using reference parameters. // void swap(int &x, int &y); int main() { int i, j; i = 10; j = 20; cout << "Initial values of i and j: "; cout << i << ' ' << j << '\n'; swap(j, i); cout << "Swapped values of i and j: "; cout << i << ' ' << j << '\n'; return 0;
16 }
17
17 /* Here, swap() is defined as using call-by-reference, 18 not call-by-value. Thus, it can exchange the two 19 arguments it is called with.*/ 20 void swap(int &x, int &y) 21 { 22 23 24 25 int temp; temp = x; // save the value at address x x = y; // put y into x
26 } What will be the values of j and i after execution of swap() function? Does the values of j and i are modified after execution of swap() function? (Execute above program and analyze the output.) Answer:
5 6 7 8 9
int main() { double val = 100.0; double newval; cout << f() << '\n'; // display val's value
18
10 11 12 13 14 15 16 17 18 19 } }
newval = f(); // assign value of val to newval cout << newval << '\n'; // display newval's value f() = 99.1; // change val's value cout << f() << '\n'; // display val's new value return 0;
Does the value of variable val is modified? How? (Execute above program and analyze the output.) Answer:
Programming Tips
Remember to #include the headers for the facilities you use. Keep common and local names short, and keep uncommon and nonlocal names longer. Avoid similar looking names. Maintain a common naming style. Choose names to reflect meaning rather than implementations. Avoid complicated expressions. Don't declare a variable until you have a value to initialize it with. Don't return pointers or references to local variables. There are some restrictions that apply to reference variables: 1. You cannot reference a reference variable 2. You cannot create arrays of references. 3. You cannot create a pointer to a reference. That is, you cannot apply the & operator to a reference. 4. References are not allowed on bit fields.
19
Practical Assignment
SET A 1. Write a program that prompts for a person's age and heart rate per minute. The program computes and displays the number of heart beats since the person was born. You may assume that there are 365 days in a year. 2. Write a program that prints the sizes of the fundamental types, a few pointer types, and a few enumerations of your choice. Use the sizeof operator. SET B 1. Write a function that counts the number of occurrences of a pair of letters in a string. The prototype of the function is as follows: int occurrences(char *str); 2. Write a function int my_atoi(const char *) that takes a string containing digits and returns the corresponding integer. SET C 1. Write a function my_itoa(int i, char b[]) that creates a string representation of integer i in array b and returns array b. 2. Write a function named TimeToSeconds() that takes a single argument that represents a time. The time is a string with the format hh:mm:ss, where hh is hours, mm is minutes, and ss is seconds. Function TimeToSeconds() returns the equivalent time in seconds. It TimeToSeconds() receives an invalid time (e.g. 02:23:67), it should use assert() to report an error. (Please note assert() is a standard micro available in C++).
Assignment Evaluation
0: Not Done [ ] 3: Needs Improvement [ ] 1: Incomplete [ ] 4: Complete [ ] 2.Late Complete [ ] 5: Well Done [ ]
20
Reading
You should read following topics before starting this exercise: 1. Concept of function overloading in C++ 2. Default arguments in C++ 3. Inline functions
Ready References
21
1 // Overload a function three times. 2 #include <iostream> 3 using namespace std; 4 void f(int i); // integer parameter 5 void f(int i, int j); // two integer parameters 6 void f(double k); // one double parameter 7 int main() 8 { 9 f(10); // call f(int) 10 f(10, 20); // call f(int, int) 11 f(12.23); // call f(double) 12 return 0; 13 } 14 void f(int i) 15 { 16 cout << "In f(int), i is " << i << '\n'; 17 } 18 void f(int i, int j) 19 { 20 cout << "In f(int, int), i is " << i; 21 cout << ", j is " << j << '\n'; 22 } 23 void f(double k) 24 { 25 cout << "In f(double), k is " << k << '\n'; 26 } This program produces the following output: In f(int), i is 10 In f(int, int), i is 10, j is 20 In f(double), k is 12.23
22
Example 2: Function overloading in C++. 1 // Create an overloaded version of abs() called myabs(). 2 #include <iostream> 3 using namespace std; 4 5 6 7 // myabs() is overloaded three ways. int myabs(int i); double myabs(double d); long myabs(long l);
8 int main() 9 { 10 cout << myabs(-10) << "\n"; 11 cout << myabs(-11.0) << "\n"; 12 cout << myabs(-9L) << "\n"; 13 return 0; 14 } 15 int myabs(int i) 16 { 17 cout << "Using integer myabs(): "; 18 if(i<0) return -i; 19 else return i; 20 } 21 double myabs(double d) 22 { 23 cout << "Using double myabs(): "; 24 if(d<0.0) return -d; 25 else return d; 26 } 27 long myabs(long l) 28 { 29 cout << "Using long myabs(): "; 30 if(l<0) return -l; 31 else return l; 32 } This program produces the following output: Using integer myabs(): 10 Using double myabs(): 11 Using long myabs(): 9
23
Example 3: Default arguments 1 #include <iostream> 2 using namespace std; 3 void clrscr(int size=25); 4 int main() 5 { 6 int i; 7 8 9 10 11 12 } for(i=0; i<30; i++ ) cout << i << '\n'; clrscr(); // clears 25 lines for(i=0; i<30; i++ ) cout << i << '\n'; clrscr(10); // clears 10 lines return 0;
13 void clrscr(int size) 14 { 15 for(; size; size--) cout << '\n'; 16 } As this program illustrates, when the default value is appropriate to the situation, no argument need be specified when calling clrscr( ). However, it is still possible to override the default and give size a different value.
24
Example 4: Use of Inline Function. 1 #include <iostream> 2 using namespace std; 3 inline int max(int a, int b) 4 { 5 return a>b ? a : b; 6 } 7 int main() 8 { 9 cout << max(10, 20); 10 cout << " " << max(99, 88); 11 return 0; 12 } As far as the compiler is concerned, the preceding program is equivalent to #include <iostream> using namespace std; int main() { cout << (10>20 ? 10 : 20); cout << " " << (99>88 ? 99 : 88); return 0; }
Programming Tips
Use overloading when functions perform conceptually the same task on different types. When overloading on integers, provide enough functions to eliminate common ambiguities. Default arguments may be provided for trailing arguments only. The small, frequently used functions must be defined as inline functions.
Practical Assignment
SET A 1. Write a C++ program to accept records of n employees and store it in array. Use array of structures. The structure of employee should be as follows: struct Employee { int EmpNo; char Name[20]; char Gender;
25
long }; Implement following search functions: int search(struct Employee arr[], int EmpNo); int search(struct Employee arr[], char Name[]); int search(struct Employee arr[], char Gender); int search(struct Employee arr[], long Salary);
Salary;
Also implement a function to return an employee who is drawing highest salary by using following function: Inline struct Employee compare(long a, long b); 2. Write a C++ program to sort n elements in ascending order. Use any sorting technique. Function overloading is expected in this program. SET B 1. Write a C++ program to encrypt data entered by user and display. Also provide a mechanism to decrypt the message. If user tries to re-encrypt the encoded text with the same key, then it will produce original text. (Use bitwise XOR and one character key) 2. Write a C++ program to convert integer number to string and string to integer using function overloading. (It is similar to atoi() and itoa() functions in C.) SET C 1. Write a C++ program to invert a two-dimenstional array.
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
26
Reading
You should read following topics before starting this exercise: Concept of Class in C++ Types of Member Functions in Class Creating Objects Array of Objects Access Specifiers Static Class Member Functions Friend Class and Friend Functions const Member Functions
Ready References
27
}; Example:
class item { int number; // variables declaration float cost; // private by default public: void getdata(int a, float b); // functions declaration void putdata(void); // using prototype };
Some Characteristics: 1. The class declaration is similar to a struct declaration. 2. The keyword class specifies that what follows is an abstract data of type class_name. The body of a class is enclosed within braces and terminated by a semicolon. The class body contains the declaration of variables and functions. 3. These functions and variables are collectively called class members. 4. These are usually grouped under two sections, namely, private and public to denote which of the members are private and which of them are public. The keywords public and private are known as visibility labels.
creates a variable x of type item. In C++, the class variables are known as Objects. Therefore, x is called an object of type item. We may also declare more then one object in one statement. Example: item x,y,z; Some Characteristics: The declaration of object is similar to that of a variable of any basic type. The necessary memory space is allocated to an object at this stage. Note : that class specification, like a structure, provides only a template and does not create any memory space for the objects.
28
Some Characteristics: We usually give a class some meaningful name, such as employee. The name now becomes new type identifier that can be used to declare instances of that class type. The class Employee contains two data members and two function members. The data members are private by default while both the functions are public by declaration. The function getdata() can be used to assign values for member variables age, name, and putdata() for displaying their values. These functions provide the only access to the data members from outside the class. This means that the data cannot be accessed by any function that is not a member of class employee.
29
The array manager contains three objects(managers), namely, manager[0], manager[1] and manager[2], of type employee class. Similarly, the foreman array contains 15 objects(foreman) and the worker array contains 75 objects(workers). Since an array of objects behaves like any other array, we can use the usual array accessing methods to access individual elements, and then the dot member operator to access the member functions. For example, the statement manager[i].putdata(); will display the data of the ith element of the array manager. That is, this statement requests the object manager[i] to invoke the member function putdata(). Some Characteristics: 1. An array of objects is stored inside the memory in the same way as a multidimensional array. 2. Note that only the space for data items of the objects is created. Member functions are stored separately an will be used by all the objects.
30
int main() { // Define an instance of the class integer in; // Store a number in.store(5); // Retrieve the number and print it printf(%d\n, in.get()); return 0; } The output is: 5
Now let's modify the function integer::get() so that one is added to the data member number before it is returned. inline int integer::get() const { // Cannot modify number return ++number; } The compiler error message is: Cannot modify a const object in function integer::get() const Some Characteristics: 1. The use of the const keyword is valid only for nonstatic class member functions. In other words, you cannot append the const keyword after a global function or a static member functions.
31
functions. This is the means by which you can communicate with a class by sending messages to the public member functions. To write an access specifier, within the class definition use the appropriate keyword followed by a colon before the class members to which the specifier applies. For Example, we can expand the definition of the integer class to include the access specifiers: class integer { private: // optional // All private members here public: // All public members here };
32
would be no single variable having a count of the total number of object created. To avoid this, you use static member variables because it is this single static variable that gets incremented by all objects. You can also initialize the static variable inside the class definition as shown in the following example. Example 2: #include <iostream> class myclass { void increment() { static int i=5; cout<<i<<endl; i++; } public: void display() { cout<<calling increment for first time<<endl; increment(); cout<<calling increment for second time<<endl; increment(); } }; int main() { myclass m1; m1.display(); return 0; } Compile and run this program to see the output.
Static Functions Like static variables, you can also declare a function to be static. Static functions can only access static variables and not non-static variables. You use the class name to access the static function, as shown below: ClassName :: StaticFunctionName; The following code samples illustrates the use of static functions:
33
Example 3: class staticExample { int data; static int staticVar; // Static variable declared public: static void display() { cout<<staticVar= << staticVar; cout<<data= <<data; // Error! Static functions // cannot access non// static variables. }; Int staticExample :: staticVar=0; // Static variable initialized to 0 void main() { staticExample:: display(); // Without creating an object of a class, the // static function can be accessed } Compile and run this program to see the output. In a situation where you want to check whether or not any object of a class has been created, you make use of static functions because they come into existence even before the object is created. Example 4: You have defined a Dealer class as part of devoluping the billing system software for Diaz Telecommunications, Inc. The class, which youve defined, is as follows: class Dealer { private: char mobileNo[11]; char dealerName[25]; char dealerAddress[51]; char dealerCity[25]; char phoneNo[11]; public: static int CompanyID; static void showed() { cout<<The dealer name is<<dealername; cout<<The company ID is <<CompanyID; } void get() { // Code to accept the dealer details }
34
} void print() { // Code to print the dealer deatails } }; int CompanyID=6519; The Dealer class definition shown above is incorrect. Identify the errors in the Dealer class and write a correct definition for it.
35
Some Characteristics: 1) friend functions are used when the design of a class requires that another class or nonmember function should access the private elements of the class. In such a case, friend function declarations may be used to enable the class to access those variables directly. 2) Friend functions may also be inline functions. That is, if the friend function is defined within the scope of class definition, then the inline request to the compiler is automatically made. If the friend function is defined outside the class definition, then you must precede the return type with the keyword inline in order to make the request. 3) Function operating on objects of two different classes. This is the ideal situation where the friend function can be used to bridge two classes. 4) Friend functions can be used to increase the versatility of overloaded operators. 5) Sometimes, a friend allows a more obvious syntax for calling a function, rather then what a member function can do. Friend Classes Objectives: Concept Need. Reading: Just as a function can be made a friend of a class, a class can also be made a friend of another class. Example: class distance { .. friend class length; .. }; In this example, the length class is the friend of the class distance. All the members of the class distance can now be accessed from the length class. The program below illustrates an entire class declared as a friend of another class. Example 5: # include<iostream> class class1 { private: int i; public: friend class class2; }; class class2 { public: void func() { class1 A1;
36
// The function can access the private data of class1 cout<<The value of i in class class1 is } }; int main() { class2 B1; B1.func(); return 0; } In the preceding program, the class2 class is a friend of the class1 class. Now, all the member functions of class2 can access the nonpublic data of class1. Compile and run this program to see the output. Some characteristics: 1) Friendship is not mutual by default. That is, once class B is declared as a friend of class A, this does not give class A the right to access the private members of the class B. 2) Friendship, when applied to program design, is an escape mechanism which creates exceptions to the rule of data hiding. Usage of friend classes should, therefore, be limited to those cases where it is absolutely essential. <<A1.i<<endl;
37
inline int integer:: get() const { return number; } int main() { // Define an instance of the class integer in; // Store a number in.store(5); // Retrieve the number and print it printf(%d\n, in.get()); return 0; } The output is: 5 Now let's modify the function integer::get() so that one is added to the data member number before it is returned. inline int integer::get() const { // Cannot modify number return ++number; } The compiler error message is: Cannot modify a const object in function integer::get() const Some Characteristics: 1. The use of the const keyword is valid only for nonstatic class member functions. In other words, you cannot append the const keyword after a global function or a static member functions.
Programming Tips
Make a function member only if it requires direct access to the representation of a class. Make a member function that doesnt modify the value of its object a const member function. Make a function that needs access to the representation of a class but need not be called for a specific object a static member function. If a class has a pointer member, it needs copy operations (copy constructor and copy assignment). Use enumerators when you need to define integer constants in class declarations. Remember temporary objects are destroyed at the end of the full expression in which they are created.
38
Practical Assignment
SET A 1. Define a class to represent a bank account. Include the following members: Data members 1) Name of the depositor 2) Account number 3) Type of account 4) Balance amount in the account. Member functions: 1) To assign initial values 2) To deposit an amount 3) To withdraw an amount after checking the balance 4) To display name and balance. Write a main program to test the program. 2. Define class Date which will provide operations like adding day, month or year to existing date. Also provide function to check for the leap year. SET B 1. Write a class to represent a vector. Include member functions to perform the following tasks: 1) To create the vector 2) To modify the value of given element. 3) To multiply by a scalar value 4) To display the vector in the form(10,20,30,) 2. Create a Hen class. Inside this, nest a Nest class. Inside Nest, place an Egg class. Each class should have a display( ) member function. In main( ), create an instance of Hen class and call the display( ) function for each one. SET C 1. Define a class named Sequence for storing sorted strings. Following are the member functions for Sequence class: Insert which inserts a new string into its sort position. Delete which deletes an existing string. Find which searches the sequence for a given string and returns true if it finds it, and false otherwise. Print which prints the sequence strings.
39
Assignment Evaluation 0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
Date of Completion
40
Reading
You should read following topics before starting this exercise: What is constructor? Role of constructor in class Types of constructor Role of destructor and how it is different from constructor?
Ready References
4.1Constructors
A constructor is special member function whose task is to initialize the objects of its class. It is special because its name is the same as the class name. Some characteristics
1. They should be declared in public section. 2. They are invoked automatically when the objects are created. 3. They do not have return types, not even the void and therefore, they cannot return
values.
41
//constructor
---------------
When we create object for above class test, it will get initialized automatically. Ex. Test obj; It will create object and initializes data members m and n to zero.
42
Example 2: class test { int x,y; public: void test() { cout<< You are in default constructor..\n; } void test(int m, int n) { x=m; y=n; } void test(float m, float n=5.3) { x=m; y=n; cout<<x<<y; } }; void main() { test obj1; test obj2(2,3); test obj3(4.4, 5.4); } Compile and run this program to see the output.
e. Copy Constructor It takes a reference to an object of the same class as itself as argument. It copies data from one object to other. It would not create an object as it is just an assignment statement. Example 3: class test { private: int k, m; public: test() { k=15; m=18; } test(test & s) { k=s.k; m=s.m; } void display() {
43
Cout<<k<<m; } }; void { main() test obj1; test obj2(obj1); test obj3=obj1; obj1.display(); obj2.display(); obj3.display(); } Output: 15 15 15 f. Dynamic Constructor This type of constructor can also be used to allocate memory while creating objects. Two operators new and delete are used in dynamic memory allocation. 18 18 18 // for obj1 // for obj2 // for obj3
Pointer
delete ptr;
Keyword pointer
44
Example 4: class { test private: int x; public: void getdata() { cout<<Enter value:\n; cin>>x; } void display() { cout<<x=<<x; } }; void main() { test *ptr; ptr=new test; ptr->getdata(); ptr->display(); delete ptr; } Compile and run this program to see the output.
4.3 Destructors
Destructor is a special member function which is called automatically whenever a class object is destroyed. Whenever any object goes out of scope of its existence, destructor will be called automatically and it takes out the allocated memory. Some characteristics A destructor name must be same as the class name in which it is declared, prefixed or preceded by a symbol tilde(~). Does not have any return type. Cannot be declared static, constant. Declared as a public member function.
45
//constructor
Programming Tips
Use a constructor to establish invariant for a class. If a constructor requires a resource, its class needs a destructor to release the resource. If a class has a pointer member or reference member, it needs copy operations (copy constructor, assignment operator). If a class needs copy operation of a destructor, it probably needs a constructor, a destructor, a copy assignment(=) and a copy constructor. When writing a copy constructor, be careful to copy every element that needs to be copied (deep copying). Beware of default initializer.
Practical Assignment
SET A 1. Write a c++ program to read a set of numbers upto n (accepted from the user) and print
the contents of the array in the reverse order using dynamic memory allocation operators new and delete.
2. Define a class named Complex for representing complex numbers. A complex number has the general form a + ib, where a is the real part and b is the imaginary part (i stands for imaginary). Complex arithmetic rules are as follows:
= = =
46
SET B 1. Write a c++ program to read the information like plant_name, plant_code, plant_type, price.Construct the database with suitable member functions for initializing and for destroying the data, viz. constructor, copy constructor, destructor. SET C 1. Define class named BinTree for storing sorted strings as a binary tree with all necessary
functions for total leaves, total nodes, traversing operations. Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
47
Overloading Unary operators as member function and friend function Overloading Binary operators as member function. and friend function Usage of this pointer Overloading >> and << operator
Reading
You should read following topics before starting the exercise: Function Overloading and polymorphism in C++ Operators in C++ and functions associated with each operator Rules for function overloading insertion (>>) and extraction(<<) operators concept of this pointer in C+
Ready References
48
.* ::
Sizeof ?:
Operators are overloaded in c++ by creating operator functions either as a member or as non member functions which are Friend Function of a class. Operator functions are declared using the following general form: ret-type operator#(arg-list); and then defining it as a normal member function. Here, ret-type is commonly the name of the class itself as the operations would commonly return data (object) of that class type. # is replaced by any valid operator such as +, -, *, /, ++, -- etc.
49
A nonmember function that overloads the same operator would have the following form: return_type operator#(T, U) An overloaded binary operator may return any type. Eg. Sample program 1
//unary operators as friend function friend MyClass operator--(MyClass &ob); // prefix friend MyClass operator--(MyClass &ob, int notused); // postfix //binary operators as friend function MyClass operator++();
//binary operators MyClass operator+(MyClass MyClass operator=(MyClass MyClass operator*(MyClass MyClass operator/(MyClass
50
friend ostream &operator<<(ostream &stream, MyClass ob); friend istream &operator>>(istream &stream, MyClass &ob);
void show() ;
}; // Overload -- (prefix) for MyClass class using a friend. MyClass operator--(MyClass &ob) { ob.x--; ob.y--; return ob; }
// Overload -- (postfix) for MyClass class using a friend. MyClass operator--(MyClass &ob, int notused) { ob.x--; ob.y--; return ob; } // Overload + relative to MyClass class. MyClass MyClass::operator+(MyClass op2) { MyClass temp; temp.x = x + op2.x; temp.y = y + op2.y; return temp; } // Overload = relative to MyClass class. MyClass MyClass::operator=(MyClass op2) { x = op2.x; // These are integer assignments y = op2.y; // and the = retains its original // meaning relative to them. return *this; } // Overload ++ relative to MyClass class. MyClass MyClass::operator++() { x++; // increment x and y y++; return *this; }
51
// Overload * relative to MyClass class. MyClass MyClass::operator*(MyClass object2) { MyClass temp; temp.x = x * object2.x; temp.y = y * object2.y; return temp; } // Overload / relative to MyClass class. MyClass MyClass::operator/(MyClass object2) { MyClass temp; temp.x = x / object2.x; temp.y = y / object2.y; return temp; } // Overload << relative to MyClass class. ostream &operator<<(ostream &stream, MyClass ob) { stream << ob.x << ' ' << ob.y << '\n'; return stream; } // Overload >> relative to MyClass class. istream &operator>>(istream &stream, MyClass &ob) { stream >> ob.x >> ob.y; return stream; }
void MyClass::show() { cout << x << ", "; cout << y << ", "; }
int main() { MyClass object1(10, 10); int x, y; --object1; // decrement object1 an object
52
cout << "(--object1) : " << endl; object1.show; object1--; // decrement object1 an object cout << "(object1--) : " << endl; object1.show;
MyClass a(1, 2), b(10, 10), c; a.show(); b.show(); c = a + b; c.show(); c = a + b + c; c.show(); c = b = a; c.show(); b.show(); ++c; c.show();
MyClass object1(10, 10), object2(5, 3), object3; object3 = object1 * object2; cout << "(object1*object2) : " << object3<< endl;
53
Example 2
// A Simple Example of a overloaded operator << and operator>> #include <iostream> using namespace std; class Point { private: double m_dX, m_dY, m_dZ; public: Point(double dX=0.0, double dY=0.0, double dZ=0.0) { m_dX = dX; m_dY = dY;
54
m_dZ = dZ; } friend ostream& operator<< (ostream &out, Point &cPoint); friend istream& operator>> (istream &in, Point &cPoint); double GetX() { return m_dX; } double GetY() { return m_dY; } double GetZ() { return m_dZ; }
};
ostream& operator<< (ostream &out, Point &cPoint) { // Since operator<< is a friend of the Point class, we can access // Point's members directly. out << "(" << cPoint.m_dX << ", " << cPoint.m_dY << ", " << cPoint.m_dZ << ")"; return out; } istream& operator>> (istream &in, Point &cPoint) { in >> cPoint.m_dX; in >> cPoint.m_dY; in >> cPoint.m_dZ; return in; } int main() { using namespace std; cout << "Enter a point: " << endl; Point cPoint; cin >> cPoint; cout << "You entered: " << cPoint << endl; } return 0;
55
Programming Tips
You cannot define new operators, such as **. You cannot redefine the meaning of operators when applied to built-in data types. Overloaded operators must either be a nonstatic class member function or a global function. A global function that needs access to private or protected class members must be declared as a friend of that class. A global function must take at least one argument that is of class or enumerated type or that is a reference to a class or enumerated type. Operators obey the precedence, grouping, and number of operands dictated by their typical use with built-in types. Unary operators declared as member functions take no arguments; if declared as global functions, they take one argument. Binary operators declared as member functions take one argument; if declared as global functions, they take two arguments. If an operator can be used as either a unary or a binary operator (&, *, +, and -), you can overload each use separately. Overloaded operators cannot have default arguments.
Practical Assignment
SET A 1. Create a class Float that contains one float data member . Overload the 4 arithmetic operators and unary +,- on the objects of Float.Also overload insertion and extraction operators. 2. Create a class Rational to represent a Rational number. Perform the Basic Arithematic operation : Addition, Subtraction, Multiplication and Division for Two Rational Numbers. SET B 1. Create a class String to represent a string. Overload the + operator to concatenate 2 strings, <=, == , >= to compare 2 strings. 2. Create a class Fraction to represent a fraction of the type 2/5. Overload the 4 arithmetic operators and <,>,!=,++,-- on the objects of Fraction. 3. Complete the implementation of the following String class. Note that two versions of
the constructor and = are required, one for initializing/assigning to a String using a char*, and one for memberwise initialization/assignment. Operator [] should index a string character using its position. Operator + should concatenate two strings.
56
class String { public: String String String ~String (const char*); (const String&); (const short); (void);
String& operator = (const char*); String& operator = (const String&); char& operator [] (const short); int Length (void) {return(len);} friend String operator + (const String&, const String&); friend ostream& operator <<(ostream&, String&); private: char short };
SET C 1. 2. 3. Create a class Matrix of size m*n to represent a matrix. Define all possible matrix operations for Matrix objects eg +,-,*,scaling. Write a program to overload the new and delete operator
Sparse matrices are used in a number of numerical methods (e.g., finite element analysis). A sparse matrix is one which has the great majority of its elements set to zero. In practice, sparse matrices of sizes up to 500 500 are not uncommon. On a machine which uses a 64-bit representation for reals, storing such a matrix as an array would require 2 megabytes of storage. A more economic representation would record only nonzero elements together with their positions in the matrix. Define a SparseMatrix class which uses a linked-list to record only nonzero elements, and overload the +, -, and * operators for it. Also define an appropriate memberwise initialization constructor and memberwise assignment operator for the class. A bit vector is a vector with binary elements, that is, each element is either a 0 or a 1. Small bit vectors are conveniently represented by unsigned integers. For example, an unsigned char can represent a bit vector of 8 elements. Larger bit vectors can be defined as arrays of such smaller bit vectors. Complete the implementation of the Bitvec class, as defined below. It should allow bit vectors of any size to be created and manipulated using the associated operators.
enum Bool {false, true}; typedef unsigned char uchar; class BitVec { public: BitVec BitVec BitVec ~BitVec BitVec& operator BitVec& operator BitVec& operator BitVec& operator
4.
(const short dim); (const char* bits); (const BitVec&); (void) { delete vec; } = (const BitVec&); &= (const BitVec&); |= (const BitVec&); ^= (const BitVec&);
57
BitVec& operator <<= (const BitVec& operator >>= (const int operator [] (const void Set (const void Reset (const BitVec operator ~ BitVec operator & BitVec operator | BitVec operator ^ BitVec operator << BitVec operator >> Bool operator == Bool operator != friendostream& operator private: uchar *vec; short bytes; };
(void); (const BitVec&); (const BitVec&); (const BitVec&); (const short n); (const short n); (const BitVec&); (const BitVec&); << (ostream&, BitVec&); // vector of 8*bytes bits // bytes in the vector
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
58
SESSION 6 Inheritance
Objectives
To learn about: To know about inheritance. Types of inheritance. To know about virtual base class. To know about abstract class. To know about virtual and pure virtual functions.
Reading
You should read following topics before starting this exercise: concept of inheritance types of inheritance polymorphism run time and compile time virtual inheritance Concept of Overriding - virtual functions and pure virtual functions
Ready References
6.1 Inheritance
Inheritance is the process of creating new classes from an existing class. The existing class is known as base class and the newly created class called as a derived class. Defining the derived class: class derived_class_name : visibility_mode base_class_name { --------------}; Example: class ABC { -----------------}; class XYZ : private ABC { -----------------};
59
Here class ABC is base class. Here class XYZ is derived class. Following table shows how the visibility of base class members undergoes modifications in all three types of derivation.
Base class Visibility public derivation Private Protected Public Not inherited Protected Public
Derived class visibility Private Derivation Not inherited Private Private Protected Derivation Not inherited Protected Protected
60
class ABC { -----------------}; class XYZ : private ABC { ----------------}; class LMN : public XYZ { --------------------4. Hierarchical Inheritance
};
This is the process where many derived classes share the properties of base class. Example: A student shows a hierarchy as Arts, medical or engineering students. The medical is again has a hierarchy as medicine, surgery, radiology, gynaec, etc. 5. Hybrid Inheritance It is referred as multiple base class of a derived class having the same base class. Example: case of multilevel inheritance where student, test and result are three classes. But suppose some one or say authority wants that some weightage should be given for sports to finalize the result.
61
Execution of base class constructor Method Of Inheritance class B : public A { }; class A : public B, public C { }; class A : public B, virtual public C { }; Order Of Execution A() : base constructor B(): derived constructor B(); base(first) C() : base(second) A(): derived C(): virtual base B() : ordinary base A(): derived
62
} }; void main() { base ob1; der ob2; cout<<bptr points to base; bptr=&ob1; bptr->disp(); // call base version bptr->show(); // call base version cout<<bptr points to derived; bptr=&ob2; bptr->disp(); // call to base bptr->show(); // call to derived } Output: bptr points to base base showbase bptr points to derived base derived
63
Programming Tips
Use pointers and references to avoid slicing. Use abstract classes to focus design on the provision of clean interfaces. Use abstract classes to minimize interfaces. Use abstract classes to keep implementation details out of interfaces. Use virtual functions to allow new implementation o be added without affecting user code. A class with a virtual function should have virtual destructor.
Practical Assignment
SET A 1. Write a program in c++ to read the following information from the keyboard in which the base class consists of employee name, code and designation. The derived class Manager which contains the data members namely, year of experience and salary. Display the whole information on the screen. 2. Create base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive three specific classes called triangle, circle and rectangle from the base shape. Add to the base class, a member function get_data() to initialize base class data members and display_area() to compute and display area. Make display_area() as a virtual function.(Hint: **use default value for one parameter while accepting values for shape circle.) SET B 1. Write a program in C++ to define class PriorityQueue. Derive two sub classes from it AscendingPriorityQueue and DescendingPriorityQueue. 2. Write a class with one virtual function and one nonvirtual function. Inherit a new class, make an object of this class, and upcast to a pointer of the base-class type. Use the clock( ) function found in <ctime> (youll need to look this up in your local C library guide) to measure the difference between a virtual call and non-virtual call.Youll need to make multiple calls to each function inside
64
your timing loop in order to see the difference. 3. Write a C++ program to maintain information about Instructor who is the student and employee of same college. Use multiple inheritance. (Derive Instructor class from Employee and Student.) SET C 1. An abstract class is a class which is never used directly but provides a skeleton for other
classes to be derived from it. Typically, all the member functions of an abstract are virtual and have dummy implementations. The following is a simple example of an abstract class:
class Database { public: virtual voidInsert virtual voidDelete virtual DataSearch };
{return 0;}
It provides a skeleton for a database-like classes. Examples of the kind of classes which could be derived from database include: linked-list, binary tree, and B-tree. Derive a B-tree class from Database:
class BTree : public Database { /*...*/ };
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
65
Managing console I/O C++ stream classes Formatted and Unformatted console I/O Usage of manipulators
Reading
You should read following topics before starting this exercise: Concept of streams in C++ (text and binary) C++ Stream Classes Concept of manipulators
Ready References
basic_streambuf This class supplies the basic, low-level input and output operations, and provides the underlying support for the entire C++ I/O system. It is required only in advanced I/O programming. basic_ios This is a high-level I/O class that provides formatting, error checking, and status information related to stream I/O. (A base class for basic_ios is called ios_base, which defines several nontemplate traits used by basic_ios.) basic_ios is used as a base for several derived classes, including basic_istream, basic_ostream, and basic_iostream. These classes are used to create streams capable of input, output, and input/output, respectively.
66
ios The ios class contains many member functions and variables that control or monitor the fundamental operation of a stream.
Streams cin, cout, and cerr correspond to C's stdin, stdout, and stderr. By default, the standard streams are used to communicate with the console.However, in environments that support I/O redirection (such as DOS, Unix, OS/2, and Windows), the standard streams can be redirected to other devices or files. For the sake of simplicity, the examples in this chapter assume that no I/O redirection has occurred. Standard C++ also defines these four additional streams: win, wout, werr, and wlog. These are wide-character versions of the standard streams. Wide characters are of type wchar_t and are generally 16-bit quantities. Wide characters are used to hold the large character sets associated with some human languages.
Setting the format flags using setf() function To set a flag, use the setf() function. This function is a member of ios. Its most common form is shown here: fmtflags setf(fmtflags flags); This function returns the previous settings of the format flags and turns on those flags specified by flags. For example, to turn on the showpos flag, you can use this statement:
67
stream.setf(ios::showpos); Here, stream is the stream you wish to affect. Notice the use of ios:: to qualify showpos. Since showpos is an enumerated constant defined by the ios class, it must be qualified by ios when it is used. Example: #include <iostream> using namespace std; int main() { cout.setf(ios::showpoint | ios::showpos); cout << 100.0; // displays +100.0 return 0; }
Example 1:
#include <iostream> using namespace std; int main() { cout.setf(ios::uppercase | ios::scientific); cout << 100.12; // displays 1.0012E+02 cout.unsetf(ios::uppercase); // clear uppercase cout << " \n" << 100.12; // displays 1.0012e+02 return 0; } Compile and run this program to see the output.
68
using namespace std; int main( ) { cout.setf(ios::showpoint | ios::showpos, ios::showpoint); cout << 100.0; // displays 100.0, not +100.0 return 0; } Compile and run this program to see the output.
The most common use of the two-parameter form of setf() is when setting the number base, justification, and format flags. Example 3: #include <iostream> using namespace std; int main() { cout.setf(ios::hex, ios::basefield); cout << 100; // this displays 64 return 0; } Compile and run this program.
Here, the basefield flags (i.,e., dec, oct, and hex) are first cleared and then the hex flag is set. Remember, only the flags specified in flags2 can be affected by flags specified by flags1. For example, in this program, the first attempt to set the showpos flag fails. Example 4: // This program will not work. #include <iostream> using namespace std; int main() { cout.setf(ios::showpos, ios::hex); // error, showpos not set cout << 100 << '\n'; // displays 100, not +100 cout.setf(ios::showpos, ios::showpos); // this is correct cout << 100; // now displays +100 return 0; } Compile and run this program.
69
70
void showflags(); int main() { // show default condition of format flags showflags(); // showpos, showbase, oct, right are on, others off long f = ios::showpos | ios::showbase | ios::oct | ios::right; cout.flags(f); // set all flags showflags(); return 0; } Compile and run this program.
71
Example 7: #include <iostream> using namespace std; int main() { cout.precision(4) ; cout.width(10); cout << 10.12345 << "\n"; // displays 10.12 cout.fill('*'); cout.width(10); cout << 10.12345 << "\n"; // displays *****10.12 // field width applies to strings, too cout.width(10); cout << "Hi!" << "\n"; // displays *******Hi! cout.width(10); cout.setf(ios::left); // left justify cout << 10.12345; // displays 10.12***** return 0; } This program's output is shown here: 10.12 *****10.12 *******Hi! 10.12***** There are overloaded forms of width() , precision() , and fill() that obtain but do not change the current setting. These forms are shown here: char fill( ); streamsize width( ); streamsize precision( );
72
left nobooalpha noshowbase noshowpoint noshowpos noskipws nounitbuf nouppercase oct resetiosflags (fmtflags f ) right scientific setbase(int base) setfill(int ch) setiosflags(fmtflags f ) setprecision (int p) setw(int w) showbase showpoint showpos skipws unitbuf uppercase ws Example 8:
Turns on left flag. Turns off boolalpha flag. Turns off showbase flag. Turns off showpoint flag. Turns off showpos flag. Turns off skipws flag. Turns off unitbuf flag. Turns off uppercase flag. Turns on oct flag. Turn off the flags specified in f. Turns on right flag. Turns on scientific flag. Set the number base to base. Set the fill character to ch. Turn on the flags specified in f. Set the number of digits of precision. Set the field width to w. Turns on showbase flag. Turns on showpoint flag. Turns on showpos flag. Turns on skipws flag. Turns on unitbuf flag. Turns on uppercase flag. Skip leading white space.
Output Input/Output Output Output Output Input Output Output Input/Output Input/Output Output Output Input/Output Output Input/Output Output Output Output Output Output Input Output Output Input
#include <iostream> #include <iomanip> using namespace std; int main() { cout << hex << 100 << endl; cout << setfill('?') << setw(10) << 2343.0; return 0; } This displays 64 ??????2343
Example 8: #include <iostream> #include <iomanip> using namespace std; int main() {
73
cout << setiosflags(ios::showpos); cout << setiosflags(ios::showbase); cout << 123 << " " << hex << 123; return 0;
Compile and run this program. Example 9: #include <iostream> using namespace std; int main() { bool b; b = true; cout << b << " " cout << "Enter a cin >> boolalpha cout << "Here is return 0; }
<< boolalpha << b << endl; Boolean value: "; >> b; what you entered: " << b;
The output is: 1 true Enter a Boolean value: false Here is what you entered: false
74
stream.setf(ios::hex, ios::basefield); return stream; } int main() { cout << 256 << " " << sethex << 256; return 0; } Compile and run this program.
Custom manipulators need not be complex to be useful. For example, the simple manipulators la() and ra() display a left and right arrow for emphasis, as shown here: Example 11: #include <iostream> #include <iomanip> using namespace std; // Right Arrow ostream &ra(ostream &stream) { stream << "-------> "; return stream; } // Left Arrow ostream &la(ostream &stream) { stream << " <-------"; return stream; } int main() { cout << "High balance " << ra << 1233.23 << "\n"; cout << "Over draft " << ra << 567.66 << la; return 0; } This program displays: High balance -------> 1233.23 Over draft -------> 567.66 <-------
parameterless input manipulator functions have this skeleton: istream &manip-name(istream &stream) {
75
// your code here return stream; } The following program creates the getpass() input manipulator, which rings the bell and then prompts for a password: Example 12: #include <iostream> #include <cstring> using namespace std; // A simple input manipulator. istream &getpass(istream &stream) { cout << '\a'; // sound bell cout << "Enter password: "; return stream; } int main() { char pw[80]; do { cin >> getpass >> pw; } while (strcmp(pw, "password")); cout << "Logon complete\n"; return 0; } Compile and run this program to see the output.
Programming Tips
Because the format flags are defined within the ios class, you must access their values by using ios and the scope resolution operator. For example, showbase by itself will not be recognized. You must specify ios::showbase. Remember that it is crucial that your manipulator return stream. If it does not, your manipulator cannot be used in a series of input or output operations. Use lower level input functions such as get() and read() only when runtime efficiency is at premium. Be careful with the termination criteria when using get(), getline() and read(). Remember that width specifications are applicable to following I/O operations only. Remember that precision specifications are applicable to following floating point output operations only.
Prefer manipulators to state flags for controlling I/O.
76
Practical Assignment
SET A 1. Write a program to accept student details and print marksheet on screen using appropriate manipulators. SET B 1. Implement an encrypt(k) manipulator that ensures that output on its ostream is encrypted using the key k. Provide the similar decrypt(k) manipulator for an istream. Provide the means for turning the encryption off for a stream so that further I/O is cleartext. NOTE: this session contains one exercise in SET A and one in SET B. SET C is not provided for this session.
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
77
Reading
You should read following topics before starting this exercise: What is exception? What is the purpose of exception handling? How C++ provides exception handling?
Ready References
Some examples for exceptions: EOF is reached while trying to read from a file Division by 0 is attempted (result is meaningless) Array subscript is out of range , Bad input
78
79
Note:
To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored. A exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block: Example 1 // C++ program using try , catch , throw #include <iostream> using namespace std; void main () { cout << "Inside Main" << endl; try { cout << "Inside try block " << endl; throw 10; } catch (int i) { cout << "Exception caught: " << i << endl; } cout << "Ending .." << endl; } Output for Example : Inside Main Inside try block Exception caught: 10 Ending.
Note:
Uncaught Exceptions A thrown exception which is not caught will cause the terminate () function to be invoked By default, this simply calls abort ()
80
All exceptions thrown by components of the C++ Standard library throw exceptions derived from this std::exception class. These are: Exception bad_alloc bad_cast bad_exception bad_typeid Description thrown by new on allocation failure thrown by dynamic_cast when fails with a referenced type thrown when an exception type doesn't match any catch thrown by typeid
81
ios_base::failure thrown by functions in the iostream library For example, if we use the operator new and the memory cannot be allocated, an exception of type bad_alloc is thrown: try {
int * myarray= new int[1000]; } catch (bad_alloc&) { cout << "Error allocating memory." << endl; } It is recommended to include all dynamic memory allocations within a try block that catches this type of exception to perform a clean action instead of an abnormal program termination, which is what happens when this type of exception is thrown and not caught. bad_alloc is derived from the standard base class exception, we can handle that same exception by catching references to the exception class: Example 3 // bad_alloc standard exception #include <iostream> #include <exception> using namespace std; int main () { try { int* myarray= new int[1000]; } catch (exception& e) { cout << "Standard exception: " << e.what() << endl; } return 0; }
Programming Tips
Dont use exceptions where more local control structures will suffice. Avoid throwing exception from copy constructor and destructor. Have main() catch and report all exceptions. Keep ordinary code and error handling code separate.
82
Practical Assignment
SET A 1) Write a C++ program that prompts the user for a positive no, If negative no. is entered, then throw an exception as no. not positive 2) Write a C++ program to print successive integers 1, 2, 3, ... StopNum - 1. Throws an integer exception when StopNum is reached. StopNum : The stopping number to be used. SET B 1.Write a C++ program for calculator which has operators as +,-,*,/. Display appropriate exceptions mesg. like divide by zero etc. and display the calculated results. 2. Write a C++ program that prompts the user to enter a sequence of numbers (floats) and then prints them in reverse order. Use stack. Give appropriate exception message as Push not done - out of space." SET C: 1. Define appropriate exceptions for the Matrix class and its functions so that they throw exceptions when errors occur, including the following: When the sizes of the operands of + and - are not identical. When the number of the columns of the first operand of * does not match the number of rows of its second operand. When the row or column specified for () is outside its range. When heap storage is exhausted?
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
83
Reading
You should read following topics before starting this exercise: Types of files supported in C++ : Text and Binary Difference between binary and text files Concept of file pointers and their manipulations in C++
Ready References
To perform file I/O, you must include the header <fstream> in your program. It defines several classes, including ifstream, ofstream, and fstream.
84
Syntax 1: File-stream-class stream-object; Stream-object .open (filename); Example: ofstream outfile; // create stream for output outfile.open(DATA); // connect stream to DATA . . outfile.close(); Syntax 2: File-stream-class stream-object; Stream-object .open (filename,mode); File mode parameters: Parameter ios::in ios::out ios::app ios::ate ios::binary ios::nocreate ios::noreplace ios::trunc Meaning Open file for reading only Open file for writing only Append to end-of-file Go to end-of-file on opening Open as binary file Does not create a new file, opens the existing one Open file if the file already exists Delete the contents of the file if it exists
85
tellp() Syntax: o o
where offset specifies the number of bytes the file pointer is to be moved from the reference position. The refposition can be any one of the following : ios::beg ios::cur ios::end indicates beginning of the file indicates current position of the pointer indicates the end of the file
Offset can be 0 or positive w.r.t. ios::beg Offset can be positive or negative w.r.t. ios::cur Offset can be 0 or negative w.r.t. ios::end The file size can be obtained using the function tellg() or tellp() when the file pointer is located at the end of the file.
where v is some variable. First parameter to the above two functions is address of the variable v, cast to type char * and second parameter is length of that variable in bytes.
Programming Tips
Use lower-level input functions such as get() and read() only when run-time efficiency is at a premium. Be careful with the termination criteria when using get(), getline(), and read().
86
Practical Assignment
SET A 1. Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive blank spaces is replaced by a single space. 2. Write a program to count the number of characters, number of words and number of lines in a file. 3. Write a program which copies a user-specified file to another user-specified file. Your
SET B: 1. A file contains a list of telephone numbers in the following form: Ajay 12345 Vijay 98765 ---------------------------
The names contain only one word and the names and telephone numbers are separated by white spaces. Write a program to read the file and output the list in two columns. The names should be left-justified and the numbers right-justified. 2. Write a program which reads a C++ source file and checks that all instances of brackets are balanced, that is, each ( has a matching ), and similarly for [] and {}, except for when they appear inside comments or strings. A line which contains an unbalanced bracket should be reported by a message such as the following sent to standard output:
'{' on line 15 has no matching '}'
SET C: 1. Write an interactive, menu-driven program that will access the file created in the above program, and implement the following tasks. a. Determine the telephone number of the specified person. b. Determine the name if a telephone number is known. c. Update the telephone number, whenever there is a change. 2. Write a program to accept file name and string. Search string in file and display entire file with markers to indicate occurrence of given string. Also show the frequency of that string. For example, if the contents of the file XYZ.DAT are as follows: Because C++ and C code are often intermixed, C++ streams I/O is sometimes mixed with the C printf() family of I/O functions. The C-style I/O functions are presented by <cstdio> and <stdio.h>. Also, because C functions can be called from C++ some programmers may prefer to
87
use the more familiar C I/O functions. The command line argument for the above program could be file name (XYZ.DAT) and string (fun). The output will be as follows: Because C++ and C code are often intermixed, C++ streams I/O is sometimes mixed with the C printf() family of I/O <fun>ctions. The C-style I/O <fun>ctions are presented by <cstdio> and <stdio.h>. Also, because C <fun>ctions can be called from C++ some programmers may prefer to use the more familiar C I/O <fun>ctions. Frequency of Occurrence of word fun is 4.
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
88
SESSION 10 Templates
Objectives
To learn about: Concept of Templates Function Templates and Class Templates Overloading of Function Templates Function Templates With Multiple Parameters Class Templates With Multiple Parameters
Reading
You should read following topics before starting this exercise: Generic Programming in C++. Compile Time and Run Time Polymorphism and Templates. Concept of Templates in C++
Ready References
10.2.1 Definining the Template The following is a function template that returns the maximum of two values:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; }
89
This template definition specifies a family of functions that returns the maximum of two values, which are passed as function parameters a and b. The type of these parameters is left open as template parameter T. As seen in this example, template parameters must be announced with syntax of the following form: template < comma-separated-list-of-parameters >
90
When we call a function template such as max() for some arguments, the template parameters are determined by the arguments we pass. If we pass two ints to the parameter types T const& , the C++ compiler must conclude that T must be int. Note that no automatic type conversion is allowed here. Each T must match exactly. For example: template <class T> inline T const& max (T const& a, T const& b); max(4,7) // OK: T is int for both arguments max(4,4.2) // ERROR: first T is int, second T is double There are three ways to handle such an error: 1. Cast the arguments so that they both match: max(static_cast<double>(4),4.2) // OK 2. Specify (or qualify) explicitly the type of T: max<double>(4,4.2) // OK
2. Call parameters, which are declared in parentheses after the function template name: max (T const& a, T const& b) // a and b are call parameters You may have as many template parameters as you like.
91
return a<b?b:a;
// maximum of two values of any type template <class T> inline T const& max (T const& a, T const& b) { return a<b?b:a; } // maximum of three values of any type template <class T> inline T const& max (T const& a, T const& b, T const& c) { return max (max(a,b), c); } int main() { ::max(7, 42, 68); // calls the template for three arguments ::max(7.0, 42.0); // calls max<double> (by argument deduction) ::max('a', 'b'); // calls max<char> (by argument deduction) ::max(7, 42); // calls the nontemplate for two ints ::max<>(7, 42); // calls max<int> (by argument deduction) ::max<double>(7, 42); // calls max<double> (no argument deduction) ::max('a', 42.7); // calls the nontemplate for two ints } Compile and run this program to see the output.
A more useful example would be to overload the maximum template for pointers and ordinary C-strings: #include <iostream> #include <cstring> #include <string> // maximum of two values of any type template <class T> inline T const& max (T const& a, T const& b) { return a < b ? b : a; } // maximum of two pointers template <class T> inline T* const& max (T* const& a, T* const& b) { return *a < *b ? b : a; }
92
// maximum of two C-strings inline char const* const& max (char const* const& a, char const* const& b) { return std::strcmp(a,b) < 0 ? b : a; } int main () { int a=7; int b=42; ::max(a,b);
std::string s="hey"; std::string t="you"; ::max(s,t); // max() for two values of type std::string int* p1 = &b; int* p2 = &a; ::max(p1,p2);
char const* s1 = "David"; char const* s2 = "Nico"; ::max(s1,s2); // max() for two C-strings }
Note that in all overloaded implementations, we pass all arguments by reference. In general, it is a good idea not to change more than necessary when overloading function templates.
93
public: // Constructor definition to accept an array of samples Samples(T vals[], int count) { free = count<100? count:100; // Don't exceed the array for(int i=0; i<free; i++) values[i] = vals[i]; // Store count number of samples } // Constructor to accept a single sample Samples(T val) { values[0] = val; // Store the sample free = 1; // Next is free } // Default constructor Samples() { free = 0; }
// Function to add a sample bool Add(T& val) { bool OK = free<100; // Indicates there is a free place if(OK) values[free++] = val; // OK true, so store the value return OK; } // Function to obtain maximum sample T Max() { T theMax = values[0]; // Set first sample as maximum for(int i=1; i<free; i++) // Check all the samples if(values[i]>theMax) theMax = values[i]; // Store any larger sample return theMax; } };
94
// Rest of the template definition... T Max();// Function to obtain maximum sample // Rest of the template definition...
This declares the Max() function within the class. You now need to create a separate function template for the definition of the member function. You must use the template class name plus the parameters in angled brackets to identify the class to which the function belongs: template<class T> T Samples<T>::Max() { T theMax = values[0]; for(int i=1; i<free; i++) if(values[i]>theMax) theMax = values[i]; return theMax; }
// Set first sample as maximum // Check all the samples // Store any larger sample
Defining a constructor or a destructor outside of the class template definition is very similar. We could write the definition of the constructor accepting an array as: template<class T> Samples<T>::Samples(T vals[], int count) { free = count<100? count:100; // Don't exceed the array for(int i=0; i<m_Free; i++) values[i] = vals[i]; // Store count number of samples } The class to which the constructor belongs is specified in the template in the same way as for an ordinary member function. Note that the constructor name doesn't require the parameter specification. You only use the parameter with the class name preceding the scope resolution operator.
95
This defines a Samples object that can store samples of type double, and the object is created with one sample stored with the value 10.0. // Trying out a class template #include <iostream> using namespace std; // Put the Box class definition here // Put the Samples class template definition here int main() { Box Boxes[] = {
// Create an array of boxes Box(8.0, 5.0, 2.0), // Initialize the boxes... Box(5.0, 4.0, 6.0), Box(4.0, 3.0, 3.0) };
// Create the Samples object to hold Box objects Samples<Box> MyBoxes(Boxes, sizeof Boxes/sizeof Box); Box MaxBox = MyBoxes.Max(); // Get the biggest box cout << endl // and output its volume << "The biggest box has a volume of " << MaxBox.Volume() << endl; return 0; } Add appropriate class definitions as mentioned in above program. Compile and run it to see the output.
96
The parameters in a class template aren't limited to types. You can also use parameters that require constants or constant expressions to be substituted in the class definition. In our Samples template, we arbitrarily defined the values array with 100 elements. We could, however, let the user of the template choose the size of the array when the object is instantiated, by defining the template as: template <class T, int Size> class Samples { private: T values[Size]; // Array to store samples int free; // Index of free location in values public: // Constructor definition to accept an array of samples Samples(T vals[], int count) { free = count<Size? count:Size; // Don't exceed the array for(int i=0; i<m_Free; i++) values[i] = vals[i]; // Store count number of samples } // Constructor to accept a single sample Samples(T val) { values[0] = val; // Store the sample free = 1; // Next is free } // Default constructor Samples() { free = 0; // Nothing stored, so first is free } // Function to add a sample bool Add(T& val) { bool OK = free<Size; // Indicates there is a free place if(OK) values[free++] = val; // OK true, so store the value return OK; } // Function to obtain maximum sample T Max() { T theMax = values[0]; // Set first sample as maximum for(int i=1; i<free; i++) // Check all the samples if(values[i]>theMax) theMax = values[i]; // Store any larger sample return theMax; } };
97
The value supplied for Size when you create an object will replace the appearance of the parameter throughout the template definition. Now we can declare the Samples object from the previous example as: Samples<Box, 3> MyBoxes(Boxes, sizeof Boxes/sizeof Box);
Programming Tips
Use templates to express containers. Prefer templates over derived classes when run-time efficiency is at a premium. Prefer derived classes over a template if adding new variants without recompilation is important. Prefer template over derived classes when no common base can be defined.
Practical Assignment
SET A 1. Implement a template function for quick sort. 2. Define a Swap function template for swapping two objects of the same type.
3. Define a class template stack which can be instantiated as follows: stack <int, 20> stk; //size of stack is 20.
SET B 1. Define template class for linked list with all necessary operations. 2. Using the Standard C++ Library vector as an underlying implementation, create a Set template class that accepts only one of each type of object that you put into it. SET C 1. Using Standard Template Library(STL), write a program to obtain the list of files present in current directory and display these files in sorted order of name, type and size.
Assignment Evaluation
0: Not Done [ ] 3:Needs Improvement [ ] 1:Incomplete [ ] 4:Complete [ ] 2.Late Complete [ ] 5:Well Done [ ]
98