Module Operator Overloading and Type Conversion. Table of Contents
|
|
- Jeffry Dixon
- 5 years ago
- Views:
Transcription
1 1 Module - 33 Operator Overloading and Type Conversion Table of Contents 1. Introduction 2. Operator Overloading 3. this pointer 4. Overloading Unary Operators 5. Overloading Binary Operators 6. Overloading Insertion and Extraction Operators 7. Manipulation of Strings using Operators 8. Rules for Overloading operators 9. Type Conversions 10. Summary Learning outcome After studying this module, you will be able to: 1. Understand about Operator Overloading 2. Study about this pointer 3. Learn about Overloading Unary Operators 4. Learn about Overloading Binary Operators 5. Get familiar with Overloading Insertion and Extraction operators 6. Study about manipulation of strings using operators 7. Get familiar with Rules for overloading operators 8. Study about Type Conversions
2 1. Introduction The Operator overloading is one of the important feature of C++. It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. The operator that is overloaded is used to perform operation on user-defined data type. C++ permits to add two variables of user-defined types with the same syntax that was applied to the basic types. 2. Operator Overloading C++ has ability to provide the operators with a unique meaning for a data type. The mechanism of giving unique meaning to an operator is known as operator overloading. The Operator overloading offers a flexible option for the creation of new definitions for most of the C++ operators. Even though the semantics of an operator can be extended, but, it is not possible to modify its syntax and the grammatical rules that manage its use such as number of operands, precedence and associativity. The Operator overloading can be done by implementing a function which can be Member Function or a Friend Function. The two types of operator overloading that are supported by C++ are Unary operator overloading and Binary operator overloading. The Operator overloading can be done with the help of a special function, called operator function. Syntax: return_type classname :: operator op(arglist) function body // task defined In the above syntax: The return-type is the type of value returned by the specified operation and op is the operator being overloaded. The op is preceded by the keyword operator, where operator op is function name. The Operator function must be either member functions (non-static) or friend function. The basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators and only one for binary operators. This is because the object used to invoke the member function is passed implicitly and therefore it is available for the member function, where as it is not the case with friend functions. The Arguments may be passed either by value or by reference. 2.1 Steps of overloading The steps that are to be followed in process of overloading are: i) Create a class that defines the data type that is to be used in the overloading operation. ii) Declare the operator function operator op() in the public part of the class. It may be either a member function or friend function. iii) Define the operator function to implement the required operations. 2.2 Operators that can be overloaded The operators which can be overloaded are: 2
3 3 Operators that can be overloaded + - * / % ^ & ~! = < > += -= *= /= %= ^= &= = << >> >>= <<= ==!= <= >= && >*, -> [] () new delete new[] delete[] 2.3 Operators that cannot be overloaded The operators that cannot be overloaded are: Class member access operators (. &.*) Scope resolution operator ( : : ) Size of operator (sizeof) Conditional operator (? : ) The operators that cannot be overloaded using friend function are =, ->, [] and () 4. Overloading Unary Operators The overloading of unary operators can be performed using: Member function Friend function 4.1 Overloading Unary Operators using Member function In case of unary operator, since it operates on only one operand, the operand itself calls the overloaded operator function. Therefore, the argument is not sent to the function. Syntax: return_type operator op() // function code Example: demo operator () demo temp; temp.num = -num; return temp; Program to implement the usage of unary minus operator #include<iostream>
4 4 using namespace std; class Minus private: int a, b, c ; public: Minus(int A, int B, int C) a = A; b = B; c = C; void display(void); void operator - ( ); ; void Minus :: display(void) cout << "\t a = " << a << endl ; cout << "\t b = " << b << endl ; cout << "\t c = " << c << endl ; void Minus :: operator - ( ) a = -a ; b = -b ; c = -c ; int main() Minus M(5, 10, -15) ; cout << "\n Before activating operator - ( )\n" ; M.display( ) ; -M ; cout << "\n After activating operator - ( )\n" ; M.display( ) ; When the above code is compiled and executed, it produces the following result: Before activating operator - ( ) a = 5 b = 10 c = -15 After activating operator - ( ) a = -5
5 5 b = -10 c = 15 Program to illustrate the usage of ++ operator overloading #include <iostream> using namespace std; class temp private: int count; public: temp():count(5) void operator ++() count=count+1; void Display() cout<<"count: "<<count; ; int main() temp t; ++t; t.display(); return 0; When the above code is compiled and executed, it produces the following result: Count : Overloading Unary Operators using friend function When unary operator is overloaded using a friend function, it is must to pass one argument to the friend function, because friend function does not have, this pointer. If an attempt is made to modify some value of an object which is passed as argument, then the friend function actually only operates on a copy, because it is passed by value. Thus it is must to work on a copy of the object, so that it returns a newly initialized object having the modified values. To work directly on the original object, the reference parameters can be used in the operator overloaded frie nd function. Program to overload increment operator using friend function
6 6 #include<iostream> using namespace std; class point private: int x,y; public: point(int i,int j) x=i; y=j; void show() cout<<"point="<<"("<<x<<","<<y<<")"<<endl; friend void operator++(point &p1); ; void operator++(point &p1) p1.x++; p1.y++; int main() point p1(2,2); p1.show(); cout<<"after increment"<<endl; ++p1; p1.show(); return 0; When the above code is compiled and executed, it produces the following result: point=(2,2) after increment point=(3,3) 5. Overloading Binary Operators The binary operator overloading can be performed using: Member function Friend function 5.1 Overloading Binary Operators using Member function When overloading binary operator using operator function as class member function, the left side operand must be an object of the class and right side operand may be either a built-in type or user
7 defined type. It is possible to have the consecutive overloading of the binary operators. The binary operators overloaded through member function takes one argument which is formal. The binary overloaded operator function takes the first object as an implicit operand and the second operand must be passed explicitly. The Function Prototype which is to be included when overloading binary operator using member function of the class should be as follows: returntype operator op(const classname&) const; The Function Definition should be as follows: returntype classname::operator op (const classname& otherobject) const //algorithm to perform the operation return (value); Program to implement the usage of binary operator overloading using member function #include<iostream> using namespace std; class complex int a,b; public: void getvalue() cout<<"enter the value of Complex Numbers a,b:"; cin>>a>>b; complex operator+(complex c) complex t; t.a=a+c.a; t.b=b+c.b; return(t); complex operator-(complex c) complex t; t.a=a-c.a; t.b=b-c.b; return(t); 7
8 8 void display() cout<<a<<"+"<<b<<"i"<<"\n"; ; int main() complex c1,c2,c3,c4; c1.getvalue(); c2.getvalue(); c3 = c1+c2; c4=c1-c2; cout<<"complex number1:\n"; c1.display(); cout<<"complex number2:\n"; c2.display(); cout<<"sum ="; c3.display(); cout<<"\ndifference ="; c4.display(); return 0; When the above code is compiled and executed, it produces the following result: Enter the value of Complex Numbers a,b: 1 3 Enter the value of Complex Numbers a,b: -2 4 Complex number1: 1+3i Complex number2: -2+4i sum =-1+7i difference =3+-1i Program to overload relational operators #include <iostream> using namespace std; class Distance private:
9 9 int feet; int inches; public: Distance() feet = 0; inches = 0; Distance(int f, int i) feet = f; inches = i; void displaydistance() cout << "F: " << feet << " I:" << inches <<endl; Distance operator- () feet = -feet; inches = -inches; return Distance(feet, inches); bool operator <(const Distance& d) if(feet < d.feet) return true; if(feet == d.feet && inches < d.inches) return true; return false; ; int main() Distance D1(11, 10), D2(5, 11); if( D1 < D2 )
10 10 cout << "D1 is less than D2 " << endl; else cout << "D2 is less than D1 " << endl; return 0; When the above code is compiled and executed, it produces the following result: D2 is less than D1 5.2 Overloading Binary Operator using friend Functions The private members of the class cannot be accessed through outside functions, but, it is possible by using friend functions. Friend functions may be used in place of member functions for overloading a binary operator. The difference is that friend function requires two arguments to be explicitly passed to it while a member function requires only one argument. The Function Prototype which is to be included when overloading binary operator using friend function of the class should be as follows: friend returntype operator op(const classname&, const classname&); The Function Definition should be as follows: returntype operator op(const classname& firstobject, const classname& secondobject) //code to perform the operation return (value); Program to implement the usage of binary operator overloading using friend function #include <iostream> using namespace std; class myclass int a; int b; public: myclass() myclass(int x,int y) a=x; b=y;
11 11 void show() cout<<a<<endl<<b<<endl; friend myclass operator+(myclass,myclass); friend myclass operator-(myclass,myclass); ; myclass operator+(myclass ob1,myclass ob2) myclass temp; temp.a = ob1.a + ob2.a; temp.b = ob1.b + ob2.b; return temp; myclass operator-(myclass ob1,myclass ob2) myclass temp; temp.a = ob1.a - ob2.a; temp.b = ob1.b - ob2.b; return temp; int main() myclass a(100,200); myclass b(10,20),c,d; c=a+b; c.show(); d = a-b; d.show(); return 0; When the above code is compiled and executed, it produces the following result: Overloading Insertion and Extraction operators
12 C++ is able to input and output the built-in data types using the stream insertion operator (<<) and extraction operator (>>). The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. It is important to make operator overloading function a friend of the class because it would be called without creating an object. Program to overload stream insertion and stream extraction operators #include <iostream> using namespace std; class Distance private: int feet; int inches; public: Distance() feet = 0; inches = 0; Distance(int f, int i) feet = f; inches = i; friend ostream &operator<<( ostream &output, const Distance &D ) output << "F : " << D.feet << " I : " << D.inches; return output; friend istream &operator>>( istream &input, Distance &D ) input >> D.feet >> D.inches; return input; ; int main() Distance D1(11, 10), D2(5, 11), D3; cout << "Enter the value of object : " << endl; 12
13 13 cin >> D3; cout << "First Distance : " << D1 << endl; cout << "Second Distance :" << D2 << endl; cout << "Third Distance :" << D3 << endl; return 0; When the above code is compiled and executed, it produces the following result: Enter the value of object : First Distance : F : 11 I : 10 Second Distance :F : 5 I : 11 Third Distance :F : 70 I : Manipulation of Strings using Operators The ANSI C language implements the strings with the help of character arrays, pointers and string functions. It does not provide operators for manipulation of strings. For example, to copy a string, it is must that, initially, the length of the string is to be found, then it has to allocate required amount of memory. Even in C++ language, these problems arises, but in C++ language, it is possible to create own definitions of the operators that can be used to modify the strings in the same way like that of decimal numbers. It is possible by a new class called as string class which helps to overload a small number of operators to work on string objects, some of them are: String comparison (==,!=, >, <, >=, <=) Example: The statement str1 == str2 can be used to compare the contents of two string objects. Stream insertion and extraction (<<, >>) Example: cout << str1 and cin >> str2 can be used to output/input string objects. Assignment (=) Example: str1 = str2 assigns str2 into str1. Strings concatenation (+, +=) Example: str1 + str2 concatenate two string objects to produce a new string object. str1 += str2 appends str2 intostr1.
14 14 Character indexing or subscripting [] The [] operator does not perform index-bound check, i.e., it is must to ensure that the index is within the bounds. To perform index-bound check, the string's at() member function can be used. Example: str[n] can be used to get the char at index n; str[n] = c can be used to modify the char at index n. 8. Restrictions on Overloaded Operators The restrictions on overloaded operators are: Operators can be overloaded only for user defined types or a mix of user defined types and fundamental types. Only the operators which are part of C++ language can be overloaded. No new operator can be created using operator overloading. The number of operands that an operator takes (arity) cannot be changed. Any overloaded operator function must have at least one operand which is user-defined type. All of the operands cannot be of basic types. If this is the case, then function must be friend function of some class. The meaning of the operator i.e. + can be overloaded to perform multiplication operation or > operator can be overloaded to perform addition operation, but their priority of the operator cannot be changed. The operator precedence cannot be changed in overloading. The operator associativity (left to right or right to left) cannot be changed in overloading. In case of overloading binary operators, left hand side operator must be an object of class when overloaded operator function is a member function of the class. Binary operators overloaded through member function of the class take one argument and overloaded through friend function take two arguments. Unary operators overloaded through member function of the class does not take any argument and overloaded through friend function must take one argument. 9. Type Conversions In many situations, it is required to convert one data type into another. The process of converting data type of a variable into other data type is known as type conversion. When conversion of int to float, char to int, double to int etc. is done, compiler also does implicit type conversion. In general, type conversion with respect to user data type, i.e., class is divided into 3 parts, they are as follows: i) Conversion from built-in types to class type. To convert basic built-in type into class types, the simple method is to define parameterized constructor which takes an argument of basic type which is to be converted to class type. The
15 constructors perform actual type conversion from the argument s type to the constructor s class type. a) Conversion of int to class type Define a one argument constructor type int as follows: demo (int) constructor body; Example: In the main(), the statement is written as demo d=10; When compiler find this statement, it come to know that both types demo and int are not compatible so it look for conversion routine which can convert an int into demo class type. When it finds there is a constructor which takes int as argument, it calls constructor, pass the int value 10 and construct the object and assign to d. b) Conversion of char* to class type Suppose if the code is as follows: string (char *a) length=strlen(a); p= new char[length+1]; strcpy(p, a); It builds a string type object from a char* type variable a. The variable length and p are data members of the class string. Once constructor is defined and it is used for conversion from char * type to string type. ii) Conversion from class type to built-in types To convert class type to built-in type, the method that is to be adopted is that simple i.e., define an operator function by the name of data type the class data type to be converted to. The general form is as follows: operator data_type() In the above syntax, the operator is the keyword and data-type is the type in which the class type will be converted to. There is no return type or argument specified for the function. It is a member of class so any data member or function can be used inside this conversion function. Any processing over data member can be done as per requirement, but in the end, as function is about to return, it is must to return a value of type data_type. Example: Conversion of Class Type to int Suppose if the code is as follows: operator int() 15
16 int x; computing steps; return x; In the main(), it is written int num=d; where d is an object of demo class type. When Compiler finds the statement, int num =d; it comes to know that both types are not compatible so it look for conversion routine that converts an object of class to int. The casting operator function must satisfy the following conditions and they are as follows: It must be a class member It must not specify a return type It must not have any arguments. Since it is a member function, it is invoked by the object and, hence the values used for conversion inside the function belong to the object invoking the function. It means the function does not need an argument. iii) Conversion from one class type to another Example Suppose if the code is as follows: objx = objy; In the above code, the objx is an object of class X and objy is an object of class Y. The class Y type data is converted to the class X type data and the converted value is assigned to the objx. Since the conversion takes place from class Y to class X, Y is known as the source class and X is known as the destination class. Such conversions between objects of different classes can be carried out by either a constructor or a conversion function. The casting operator function should be as follows: operator typename() The above function converts the class object of which it is a member to typename. The typename may be a built-in type or a userdefined one (another class type). In case of conversion between objects, typename refers to the destination class. When class needs to be converted, a casting operator function can be used (i.e. source class). The conversion takes place in the source class and the result is given to the destination class object. In case of a single-argument, constructor function serves as an instruction for converting the argument s type to the class type of which it is member. It implies that the arrangement belongs to the source class and is passed to the destination class for conversion. It should be remembered that it is necessary that the conversion constructor be placed in the destination class. When a conversion using a constructor is performed in the destination class, it must able to access the data members of the object sent (by source class) as an argument, since data members of the source class are private, it is must to use special access functions in the source class to facilitate its data flow to the destination class. Type conversion from one class to another class can be represented in the figure as follows: 16
17 17 Type conversion from one class to another class can be represented in the table as follows: 10. Summary C++ has ability to provide the operators with a unique meaning for a data type. The mechanism of giving unique meaning to an operator is known as operator overloading. The Operator overloading can be done by implementing a function which can be Member Function or a Friend Function. The two types of operator overloading that are supported by C++ Unary operator overloading and Binary operator overloading. The Operator overloading can be done with the help of a special function, called operator function. The basic difference between them is that a friend function will have only one argument for unary operators and two for binary operators, while a member function has no arguments for unary operators and only one for binary operators. The operators that cannot be overloaded are Class member access operators (. &.*), Scope resolution operator ( : : ), Size of operator (sizeof), Conditional operator (? : ) The operators that cannot be overloaded using friend function are =, ->, [] and () Every object in C++ has access to its own address through an important pointer called this pointer. this pointer is an implicit parameter to all member functions, therefore, inside a member function, it may be used to refer to the invoking object.
18 18 this pointer is not available in static member functions as static member functions can be called without any object. In case of unary operator, since it operates on only one operand, the operand itself calls the overloaded operator function. When unary operator is overloaded using a friend function, it is must to pass one argument to the friend function, because friend function does not have, this pointer. To work directly on the original object, the reference parameters can be used in the operator overloaded friend function. When overloading binary operator using operator function as class member function, the left side operand must be an object of the class and right side operand may be either a built-in type or user defined type. The binary overloaded operator function takes the first object as an implicit operand and the second operand must be passed explicitly. The private members of the class cannot be accessed through outside functions, but, it is possible by using friend functions. The difference is that friend function requires two arguments to be explicitly passed to it while a member function requires only one argument. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. It is possible by a new class called as string class which helps to overload a small number of operators to work on string objects. The [] operator does not perform index-bound check, i.e., it is must to ensure that the index is within the bounds. Only the operators which are part of C++ language can be overloaded. No new operator can be created using operator overloading. The operator precedence cannot be changed in overloading. The process of converting data type of a variable into other data type is known as type conversion. To convert basic built-in type into class types, the simple method is to define parameterized constructor which takes an argument of basic type which is to be converted to class type. To convert class type to built-in type, the method that is to be adopted is that simple i.e., define an operator function by the name of data type the class data type to be converted to. When a conversion using a constructor is performed in the destination class, it must able to access the data members of the object sent (by source class) as an argument, since data members of the source class are private, it is must to use special access functions in the source class to facilitate its data flow to the destination class.
UNIT POLYMORPHISM
UNIT 3 ---- POLYMORPHISM CONTENTS 3.1. ADT Conversions 3.2. Overloading 3.3. Overloading Operators 3.4. Unary Operator Overloading 3.5. Binary Operator Overloading 3.6. Function Selection 3.7. Pointer
More informationObject-Oriented Design (OOD) and C++
Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign
More informationOverloading Operators in C++
Overloading Operators in C++ C++ allows the programmer to redefine the function of most built-in operators on a class-by-class basis the operator keyword is used to declare a function that specifies what
More informationCS35 - Object Oriented Programming
Syllabus CS 35 OBJECT-ORIENTED PROGRAMMING 3 0 0 3 (Common to CSE & IT) Aim: To understand the concepts of object-oriented programming and master OOP using C++. UNIT I 9 Object oriented programming concepts
More informationOperator overloading: extra examples
Operator overloading: extra examples CS319: Scientific Computing (with C++) Niall Madden Week 8: some extra examples, to supplement what was covered in class 1 Eg 1: Points in the (x, y)-plane Overloading
More informationData type of a pointer must be same as the data type of the variable to which the pointer variable is pointing. Here are a few examples:
Unit IV Pointers and Polymorphism in C++ Concepts of Pointer: A pointer is a variable that holds a memory address of another variable where a value lives. A pointer is declared using the * operator before
More informationRecharge (int, int, int); //constructor declared void disply();
Constructor and destructors in C++ Constructor Constructor is a special member function of the class which is invoked automatically when new object is created. The purpose of constructor is to initialize
More informationOBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe
OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++
More informationW3101: Programming Languages C++ Ramana Isukapalli
Lecture-6 Operator overloading Namespaces Standard template library vector List Map Set Casting in C++ Operator Overloading Operator overloading On two objects of the same class, can we perform typical
More informationUNIT- 3 Introduction to C++
UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage
More informationObjectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program
Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++
Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates
More informationChapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction
Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers
More informationReference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that
Reference Parameters There are two ways to pass arguments to functions: pass-by-value and pass-by-reference. pass-by-value A copy of the argument s value is made and passed to the called function. Changes
More informationKOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming
KOM3191 Object Oriented Programming Dr Muharrem Mercimek 1 OPERATOR OVERLOADING KOM3191 Object-Oriented Programming KOM3191 Object Oriented Programming Dr Muharrem Mercimek 2 Dynamic Memory Management
More informationAN OVERVIEW OF C++ 1
AN OVERVIEW OF C++ 1 OBJECTIVES Introduction What is object-oriented programming? Two versions of C++ C++ console I/O C++ comments Classes: A first look Some differences between C and C++ Introducing function
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationOBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak
OBJECT ORIENTED PROGRAMMING Ms. Ajeta Nandal C.R.Polytechnic,Rohtak OBJECT ORIENTED PARADIGM Object 2 Object 1 Data Data Function Function Object 3 Data Function 2 WHAT IS A MODEL? A model is an abstraction
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationJayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT
CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT Two Mark Questions UNIT - I 1. DEFINE ENCAPSULATION. Encapsulation is the process of combining data and functions
More informationC++ 8. Constructors and Destructors
8. Constructors and Destructors C++ 1. When an instance of a class comes into scope, the function that executed is. a) Destructors b) Constructors c) Inline d) Friend 2. When a class object goes out of
More informationOBJ. ORI.& MULT. PROG., M.C.Q. BANK, FOR UNIT -2, SECOND YEAR COMP. ENGG. SEM-4, 2012 PATTERN, U.O.P. UNIT-2
UNIT-2 Syllabus for Unit-2 Introduction, Need of operator overloading, overloading the assignment, binary and unary operators, overloading using friends, rules for operator overloading, type conversions
More informationCS304 Object Oriented Programming Final Term
1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing
More informationAbstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles
Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can
More informationCS 247: Software Engineering Principles. ADT Design
CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data
More informationObjectives. In this chapter, you will:
Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about
More informationCONSTRUCTORS AND DESTRUCTORS
UNIT-II CONSTRUCTORS AND DESTRUCTORS Contents: Constructors Default constructors Parameterized constructors Constructor with dynamic allocation Copy constructor Destructors Operator overloading Overloading
More informationC++ Programming: From Problem Analysis to Program Design, Third Edition
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine
More informationJAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)
Estd: 1994 JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli - 621014 (An approved by AICTE and Affiliated to Anna University) ISO 9001:2000 Certified Subject Code & Name : CS 1202
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationReview. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?
Review Describe pass-by-value and pass-by-reference Why do we use pass-by-reference? What does the term calling object refer to? What is a const member function? What is a const object? How do we initialize
More informationCAAM 420 Fall 2012 Lecture 29. Duncan Eddy
CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................
More informationGovernment Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING
Government Polytechnic, Muzaffarpur. Name of the Lab: OBJECT ORIENTED PROGRAMMING THROUGH C++ Practical: OOPS THROUGH C++ Subject Code: 1618407 PROGRAM NO.1 Programming exercise on executing a Basic C++
More informationObject Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year
Object Oriented Programming Assistant Lecture Omar Al Khayat 2 nd Year Syllabus Overview of C++ Program Principles of object oriented programming including classes Introduction to Object-Oriented Paradigm:Structures
More informationBy the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program
1 By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program variables. Apply C++ syntax rules to declare variables, initialize
More informationCHAPTER 4 FUNCTIONS. 4.1 Introduction
CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main
More informationIntroduction to C++ Systems Programming
Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions
More informationDHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING
DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING Unit I : OVERVIEW PART A (2 Marks) 1. Give some characteristics of procedure-oriented
More informationReview: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements
Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja
More information1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.
1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format
More informationProgram construction in C++ for Scientific Computing
1 (26) School of Engineering Sciences Program construction in C++ for Scientific Computing 2 (26) Outline 1 2 3 4 5 6 3 (26) Our Point class is a model for the vector space R 2. In this space, operations
More informationOperator overloading
1 Introduction 2 The copy constructor 3 Operator Overloading 4 Eg 1: Adding two vectors 5 The -> operator 6 The this pointer 7 Overloading = 8 Unary operators 9 Overloading for the matrix class 10 The
More informationCome and join us at WebLyceum
Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad
More informationC++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.
EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic
More informationADTs & Classes. An introduction
ADTs & Classes An introduction Quick review of OOP Object: combination of: data structures (describe object attributes) functions (describe object behaviors) Class: C++ mechanism used to represent an object
More informationCS242 COMPUTER PROGRAMMING
CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing
More informationOBJECT ORIENTED PROGRAMMING AND DATA STRUCTURES
Polymorphism: It allows a single name/operator to be associated with different operations depending on the type of data passed to it. An operation may exhibit different behaviors in different instances.
More informationInstantiation of Template class
Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationOutline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor
Outline EDAF50 C++ Programming 4. Classes Sven Gestegård Robertz Computer Science, LTH 2018 1 Classes the pointer this const for objects and members Copying objects friend inline 4. Classes 2/1 User-dened
More informationIntroduction Of Classes ( OOPS )
Introduction Of Classes ( OOPS ) Classes (I) A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class.
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien Deantoni adapted from Jean-Paul Rigault courses This Week A little reminder Constructor / destructor Operator overloading Programmation
More informationCS201 Some Important Definitions
CS201 Some Important Definitions For Viva Preparation 1. What is a program? A program is a precise sequence of steps to solve a particular problem. 2. What is a class? We write a C++ program using data
More informationPESIT Bangalore South Campus
USN 1 P E PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of ECE INTERNAL ASSESSMENT TEST 2 Date : 03/10/2017 Marks: 40 Subject & Code : Object Oriented Programming
More informationA <Basic> C++ Course
A C++ Course 5 Constructors / destructors operator overloading Julien DeAntoni adapted from Jean-Paul Rigault courses 1 2 This Week A little reminder Constructor / destructor Operator overloading
More informationPOINTERS - Pointer is a variable that holds a memory address of another variable of same type. - It supports dynamic allocation routines. - It can improve the efficiency of certain routines. C++ Memory
More informationDepartment of Computer science and Engineering Sub. Name: Object oriented programming and data structures Sub. Code: EC6301 Sem/Class: III/II-ECE Staff name: M.Kavipriya Two Mark Questions UNIT-1 1. List
More informationDistributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes
Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented
More informationPART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.
Unit - III CHAPTER - 9 INTRODUCTION TO C++ Choose the correct answer. PART I 1. Who developed C++? (a) Charles Babbage (b) Bjarne Stroustrup (c) Bill Gates (d) Sundar Pichai 2. What was the original name
More informationSRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR
SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR 603203 DEPARTMENT OF COMPUTER SCIENCE & APPLICATIONS QUESTION BANK (2017-2018) Course / Branch : M.Sc CST Semester / Year : EVEN / II Subject Name
More informationEvolution of Programming Languages
Evolution of Programming Languages 40's machine level raw binary 50's assembly language names for instructions and addresses very specific to each machine 60's high-level languages: Fortran, Cobol, Algol,
More informationProgramming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1
300580 Programming Fundamentals 3 With C++ Variable Declaration, Evaluation and Assignment 1 Today s Topics Variable declaration Assignment to variables Typecasting Counting Mathematical functions Keyboard
More informationIS0020 Program Design and Software Tools Midterm, Fall, 2004
IS0020 Program Design and Software Tools Midterm, Fall, 2004 Name: Instruction There are two parts in this test. The first part contains 22 questions worth 40 points you need to get 20 right to get the
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 7 September 21, 2016 CPSC 427, Lecture 7 1/21 Brackets Example (continued) Storage Management CPSC 427, Lecture 7 2/21 Brackets Example
More informationObject-Oriented Principles and Practice / C++
Object-Oriented Principles and Practice / C++ Alice E. Fischer June 3, 2013 OOPP / C++ Lecture 9... 1/40 Const Qualifiers Operator Extensions Polymorphism Abstract Classes Linear Data Structure Demo Ordered
More informationFunction Overloading
Function Overloading C++ supports writing more than one function with the same name but different argument lists How does the compiler know which one the programmer is calling? They have different signatures
More informationCSCE 110 PROGRAMMING FUNDAMENTALS
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 2. Overview of C++ Prof. Amr Goneid, AUC 1 Overview of C++ Prof. Amr Goneid, AUC 2 Overview of C++ Historical C++ Basics Some Library
More informationCS304 Object Oriented Programming
1 CS304 Object Oriented Programming 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes?
More informationOperator Overloading in C++ Systems Programming
Operator Overloading in C++ Systems Programming Operator Overloading Fundamentals of Operator Overloading Restrictions on Operator Overloading Operator Functions as Class Members vs. Global Functions Overloading
More informationEngineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs C++, Second Edition, J. Ingber 1 Simple C++ Programs Program Structure Constants and Variables C++ Operators Standard Input
More informationOperator Overloading
Operator Overloading Introduction Operator overloading Enabling C++ s operators to work with class objects Using traditional operators with user-defined objects Requires great care; when overloading is
More informationFor Teacher's Use Only Q No Total Q No Q No
Student Info Student ID: Center: Exam Date: FINALTERM EXAMINATION Spring 2010 CS201- Introduction to Programming Time: 90 min Marks: 58 For Teacher's Use Only Q No. 1 2 3 4 5 6 7 8 Total Marks Q No. 9
More information7.1 Optional Parameters
Chapter 7: C++ Bells and Whistles A number of C++ features are introduced in this chapter: default parameters, const class members, and operator extensions. 7.1 Optional Parameters Purpose and Rules. Default
More informationCSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York
CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this
More informationCSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms
CSC 330 Object Oriented Programming Operator Overloading Friend Functions & Forms 1 Restrictions on Operator Overloading Most of C++ s operators can be overloaded. Operators that can be overloaded + -
More informationRoxana Dumitrescu. C++ in Financial Mathematics
Roxana Dumitrescu C++ in Financial Mathematics What have we learnt? Arrays; relation between arrays and pointers.. Returning arrays from functions Passing arrays to functions Intoduction to classes Plan
More informationModern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi
Modern C++ for Computer Vision and Image Processing Igor Bogoslavskyi Outline Move semantics Classes Operator overloading Making your class copyable Making your class movable Rule of all or nothing Inheritance
More informationSFU CMPT Topic: Classes
SFU CMPT-212 2008-1 1 Topic: Classes SFU CMPT-212 2008-1 Topic: Classes Ján Maňuch E-mail: jmanuch@sfu.ca Friday 15 th February, 2008 SFU CMPT-212 2008-1 2 Topic: Classes Encapsulation Using global variables
More informationProgramming. C++ Basics
Programming C++ Basics Introduction to C++ C is a programming language developed in the 1970s with the UNIX operating system C programs are efficient and portable across different hardware platforms C++
More informationTokens, Expressions and Control Structures
3 Tokens, Expressions and Control Structures Tokens Keywords Identifiers Data types User-defined types Derived types Symbolic constants Declaration of variables Initialization Reference variables Type
More informationFast Introduction to Object Oriented Programming and C++
Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming
More informationPage. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team
Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team Question No: 1 ( Marks: 2 ) Write a declaration statement for an array of 10
More informationProgramming in C++: Assignment Week 4
Programming in C++: Assignment Week 4 Total Marks : 20 March 22, 2017 Question 1 Using friend operator function, which set of operators can be overloaded? Mark 1 a.,, , ==, = b. +, -, /, * c. =,
More informationChapter 18 - C++ Operator Overloading
Chapter 18 - C++ Operator Overloading Outline 18.1 Introduction 18.2 Fundamentals of Operator Overloading 18.3 Restrictions on Operator Overloading 18.4 Operator Functions as Class Members vs. as friend
More information1. In C++, reserved words are the same as predefined identifiers. a. True
C++ Programming From Problem Analysis to Program Design 8th Edition Malik TEST BANK Full clear download (no formatting errors) at: https://testbankreal.com/download/c-programming-problem-analysis-program-design-8thedition-malik-test-bank/
More informationSTUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming
More informationWhere do we go from here?
Where do we go from here? C++ classes and objects, with all the moving parts visible operator overloading templates, STL, standards, Java components, collections, generics language and performance comparisons
More informationObject Oriented Design
Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator
More informationWelcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:
Welcome Back CSCI 262 Data Structures 2 - Review What you learned in CSCI 261 (or equivalent): Variables Types Arrays Expressions Conditionals Branches & Loops Functions Recursion Classes & Objects Streams
More informationOperation Overloading.
Operation Overloading pm_jat@daiict.ac.in Recap: Why do we need Operator Overloading? Operator based expressions are more readable: Compare a + b * c with plus(a, times(b, c)) Recap: What is Operator Overloading?
More informationChapter 2 Basic Elements of C++
C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion
More informationOperators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur
Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators
More informationG52CPP C++ Programming Lecture 17
G52CPP C++ Programming Lecture 17 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Exceptions How to throw (return) different error values as exceptions And catch the exceptions
More informationI Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.
I Internal Examination Sept. 2018 Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs. [I]Very short answer questions (Max 40 words). (5 * 2 = 10) 1. What is
More information1. Describe History of C++? 2. What is Dev. C++? 3. Why Use Dev. C++ instead of C++ DOS IDE?
1. Describe History of C++? The C++ programming language has a history going back to 1979, when Bjarne Stroustrup was doing work for his Ph.D. thesis. One of the languages Stroustrup had the opportunity
More informationPolymorphism Part 1 1
Polymorphism Part 1 1 What is Polymorphism? Polymorphism refers to a programming language s ability to process objects differently depending on their data type or class. Number person real complex kid
More informationData Structures using OOP C++ Lecture 3
References: th 1. E Balagurusamy, Object Oriented Programming with C++, 4 edition, McGraw-Hill 2008. 2. Robert L. Kruse and Alexander J. Ryba, Data Structures and Program Design in C++, Prentice-Hall 2000.
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationPreface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS
Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...
More informationDarshan Institute of Engineering & Technology for Diploma Studies
1. Explain Call by Value vs. Call by Reference Or Write a program to interchange (swap) value of two variables. Call By Value In call by value pass value, when we call the function. And copy this value
More information