Lecture Notes CS102 Part1
Lecture Notes CS102 Part1
PROGRAMMING WITH
C++ (Complete)
z Programming is action-oriented
z Function is basic unit of programming
z Classes
z Inheritance
z Polymorphism
z Dynamic binding
z Message passing
Objects Objects
User
Behaviors State
item y;
cout << “\n Object y ” << “\n”;
y.getdata(200,285.99);
y.putdata();
}
Deepak Gour, Faculty- Department of IT 17
SPSSE, Udaipur
Class Definition (contd..)
//Fraction abstract data type (ADT) definition
class Fraction{
Objects Objects
User
Behaviors State
Abstraction
Data Hiding
Deepak Gour, Faculty- Department of IT 24
SPSSE, Udaipur
Encapsulation
z Data abstraction allow programmers to hide data
representation details behind a (comparatively)
simple set of operations (an interface)
z What the benefits of data abstraction?
z Reduces conceptual load
z Programmers need to knows less about the rest of the
program
z Provides fault containment
z Bugs are located in independent components
z Provides a significant degree of independence of
program components
z Separate the roles of different programmer
Software
Engineering
Deepak Gour, Faculty- Department of IT 25
Goals SPSSE, Udaipur
Encapsulation (contd..)
z The wrapping up of data and functions
into a single unit (called class) is known
as ENCAPSULATION. The data is not
accessible to the outside world, and only
those functions which are wrapped in the
class can access it. These functions
provide the interface between the object’s
data and the program.
z This insulation of the data from direct
access by the program is called DATA
HIDING or INFORMATON HIDING.
Deepak Gour, Faculty- Department of IT 26
SPSSE, Udaipur
ABSTRACTION
z ABSTRACTION refers to the act of
representing essential features without
including the background details or
explanations. CLASSES use the concept
of abstraction and are defined as a list of
abstract attributes and functions to
operate on these attributes.
Objects Objects
Behaviors State
User
Objects Objects
Behaviors State
Include files
Class declaration
Member functions definitions
Main function program
Mantissa e exponent
Signed Unsigned
z Char
z Signed characters
z Unsigned characters
z Float
z Double
z Long double
Units batch1;
Marks name1[50];
V1 = value3;
- Subtraction or unary
minus
* Multiplication
/ Division
% Modulo divisions
Deepak Gour, Faculty- Department of IT 91
SPSSE, Udaipur
TABLE: RELATIONAL
OPERATORS
Operators Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Is equal to
!= Is not equal to
Deepak Gour, Faculty- Department of IT 92
SPSSE, Udaipur
TABLE: LOGICAL OPERATOR
Operators Meaning
|| Logical OR
! Logical NOT
A=a+1 A+=1
A=a-1 a-=1
A=a*(n+1) A*=n+1
A=a/(n+1) a/=n+1
A=a%b A%=b
Deepak Gour, Faculty- Department of IT 94
SPSSE, Udaipur
INCREMENT & DECREMENT
OPERATORS
Rules for Increment and decrement operators
z Increment and decrement operators are unary
operators and they require variables as their
operands.
z When postfix ++ (or--) is used with a variable in an
expression, the expressions is evaluated first using
the original value of the variable and then the
variable is incremented (or decremented) by one.
z When prefix ++ (or--) is used in an expression, the
variable is incremented (or decremented) first and
then the expression is evaluated using the new
value of the variable.
z The precedence and associatively of ++ and --
operators are the same as those of unary + and
unary -. Deepak Gour, Faculty- Department of IT 95
SPSSE, Udaipur
CONDITIONAL OPERATORS
z A ternary operator pair “? :” is available in C
to construct conditional expressions of the
form
Exp1 ? exp2 : exp3 where exp1, exp2, and
exp3 are expressions.
The operator ? : works as follows:
z Exp1 is evaluated first. If it is nonzero (true),
then the expression exp2 is evaluated and
becomes the value of the expression. If exp1
is false, then exp3 is evaluated becomes the
value of the expression.
| Bitwise OR
^ Bitwise exclusive OR
Example:
delete pointer_variable;
delete p; delete q;
cout<<setw(width)<<variable_name<< endl;
x = (y=10); or x = y =10;
A chained statement cannot be used to
initialize a variable at the time of
declaration. For e.g.
Compound Assignment:
For e.g.
float amount (float p, int t, float r =0.15);
calling
value = amount (5000, 7);
value = amount (5000, 7, 0.12)
Note: only the trailing arguments can have
default values and therefore we must add
defaults from right to left.
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
Deepak Gour, Faculty- Department of IT 170
SPSSE, Udaipur
INHERITANCE (contd..)
class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};
not-members Yes No No
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
A B
B C D
B C
student
test sports
result
Grandparent
Parent 1 Parent 2
Child
test sports
result
#include <iostream.h>
class space
{ int x,y,z;
public:
void getdata(int a, int b, int c);
void display(void);
void operator -- ();
};
Deepak Gour, Faculty- Department of IT 232
SPSSE, Udaipur
OPERATOR OVERLOADING (contd..)
void getdist()
{ cout << “Enter feet: ”; cin >> feet;
cout << “Enter inches: ” ; cin >> inches; }
Deepak Gour, Faculty- Department of IT 242
SPSSE, Udaipur
OPERATOR OVERLOADING (contd..)
void showdist()
{ cout << feet << “ ” << inches; }
distance operator + (distance);
};
distance distance:: operator + (distance d2)
{int f = feet + d2.feet; float i = inches + d2.inches;
if (i >= 12.0) {i-=12.0; f++; } return distance(f,i);
}
Deepak Gour, Faculty- Department of IT 243
SPSSE, Udaipur
OPERATOR OVERLOADING (contd..)
void main()
*pointer_variable;
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
for (i=0;i<size;i++)
{ cout << “item: ” << i+1;
d->show();
}
return 0;
}
#include <iostream.h>
#include <cstring.h>
class person
{ char name[20]; float age;
public:
person (char *s, float a)
{strcpy(name, s); age=a; }
person & person :: greater (person & x)
{ if (x.age >= age) return x
else return *this; }
void display()
{cout << “Name: ” << name << “Age: ”}
};
void main()
{ person P1(“Kapil”, 22.25), P2(“Kartik”, 20.50),
P3(“Lakhan”, 19.50);
person P = P1.greater(P3);
cout << “Elder person is:”
P.display();
person P = P1.greater(P2);
cout << “Elder person is:”
P.display();
}
Deepak Gour, Faculty- Department of IT 277
SPSSE, Udaipur
VIRTUAL FUNCTION
When we use the same function name in both the
base and derived class, the function in base class
is declared as virtual using the keyword virtual
preceding its normal declaration. When a function
is made virtual, C++ determines which function to
use at run time based on the type of object
pointed to by the base pointer, rather than the
type of the pointer. Thus by making the base
pointer to point to different objects, we can
execute different versions of the virtual function.
cout.write(line, size);
Filebuf Its purpose is to set the file buffers to read and write. Contains
Openprot constant used in the open() of file stream classes.
Also contain close() and open() as members.
Fstreambase Provides operations common to the file streams. Serves as a
base of fstream, ifstream, and ofstream class. Contains open()
and close() functions.
Ifstream Provides input operations. Inherits get(), getline(), read(), seekg()
and tellg() functions from istream.
fin2.getline(line,size);
cout << line;
}
}
The output will be shown like:
Capital of United states of america
Washington
--------- and so on.
While (fin)
An ifstream object, such as fin, returns a value
of 0 if any error occurs in the file operation
including the eof condition. Thus the while
loop terminates when fin returns a value of
0 on reaching the eof condition.
Exercise:
Program 11.4
Program 11.5
(from Object Oriented Programming with C++,
Third Edition by E Balagurusamy)
Deepak Gour, Faculty- Department of IT 319
SPSSE, Udaipur
WORKING WITH FILES (contd.)
Reading and Writing a Class Object:
One of the shortcoming of the I/O system in
C is that it cannot handle user-defined
data types such as class objects. Since
the class objects are the central
elements of C++ programming, it is
quite natural that the language
supports features for writing to and
reading from the disk file objects
directly.
Deepak Gour, Faculty- Department of IT 320
SPSSE, Udaipur
WORKING WITH FILES (contd.)
The binary input and output functions read() and
write() are designed to do exactly this job. These
functions handles the entire structure of an object
as a single unit.
One point to remember is that only data members are
written to the disk file and the members are not.
Exercise:
Program 11.6
(from Object Oriented Programming with C++, Third
Edition by E Balagurusamy)
1. TRY
2. THROW
3. CATCH