Академический Документы
Профессиональный Документы
Культура Документы
Outline
• Operators and its types
• Operators Overloading
• Unary operators Overloading
• Binary operators Overloading
• Introduction to Inheritance
• Types of inheritance
• Introduction to Polymorphism
• Virtual and Pure Virtual Functions
• Abstract Base classes and Concrete derived classes
1
Operators and its types
• Assignment (=)
• The assignment operator assigns a value to a variable. a =
5;
• This statement assigns the integer value 5 to the variable a.
The part at the left of the assignment operator (=) is known
as the lvalue (left value) and the right one as the rvalue
(right value). The lvalue has to be a variable whereas the
rvalue can be either a constant, a variable, the result of an
operation or any combination of these.
The most important rule when assigning is the right-to-left
rule: The assignment operation always takes place from
right to left, and never the other way:
a = b;
Operators and its types
(con’t)
• Arithmetic operators ( +, -, *, /, % )
• The five arithmetical operations supported by the C++ language
are: + (addition),
- (subtraction), * (multiplication), /(division), %(modulo)
• Operations of addition, subtraction, multiplication and division
literally correspond with their respective mathematical operators.
The only one that you might not be so used to see may be
modulo; whose operator is the percentage sign (%). Modulo is the
operation that gives the remainder of a division of two values. For
example, if we write:
• a = 11 % 3; the variable a will contain the value 2, since 2 is the
remainder from dividing 11 between 3.
• Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=,
^=, |=)
• When we want to modify the value of a variable by performing an
operation on the value currently stored in that variable we can use
compound assignment operators:
Expression is Equivalent to
value += increase; value = value + increase;
a -= 5; a = a - 5;
a /= b; a = a / b;
Operators and its types
(con’t)
• Increase and decrease (++, --)
• Shortening even more some expressions, the increase
operator (++) and the decrease operator (--) increase or
reduce by one the value stored in a variable. They are
equivalent to +=1 and to -=1, respectively.
• Thus: c++; c+=1; c=c+1; are all equivalent in its
functionality: the three of them increase by one the value
of c.
• For example:
• Example 1 Example 2
B=3; B=3;
A=++B; A=B++;
// A contains 4, B contains 4 // A contains 3, B
contains 4
Operators and its types
(con’t)
• Relational and equality operators ( ==, !=, >, <, >=,
<=)
• In order to evaluate a comparison between two expressions
we can use the relational and equality operators. The result
of a relational operation is a Boolean value that can only be
true or false, according to its Boolean result.
• We may want to compare two expressions, for example, to
know if they are equal or if one is greater than the other is.
Here is a list of the relational and equality operators that
can be used in C++:
• == Equal to
• != Not equal to
• > Greater than
• < Less than
• >= Greater than or equal to
• <= Less than or equal to
Operators and its types
(con’t)
• Logical operators ( !, &&, || )
• The Operator ! is the C++ operator
to perform the Boolean operation
NOT, it has only one operand,
located at its right, and the only
thing that it does is to inverse the
value of it, producing false if its
operand is true and true if its
operand is false. Basically, it returns
the opposite Boolean value of
evaluating its operand.
Operators and its types
(con’t)
• Conditional operator ( ? )
• The conditional operator evaluates an expression
returning a value if that expression is true and a
different one if the expression is evaluated as
false. Its format is:
condition ? result1 : result2
Member2
Base class
Member1
Member2
• In the figure, the arrow shows that the derived class can access the
members of base class but the base class cannot access members of it
derived class
Categories of inheritance
• Single inheritance
• Multiple inheritance
• In single inheritance, the new class is
derived from only one base class
• In multiple inheritance, the new class
is derived from more than one base
classes
Protected Access Specifier
• Public members of a class are accessible by
all functions in the program
• Private members of a class are accessible
only by member functions and friend
functions of that class
• Protected members of the class are
accessible by the member functions and
friend functions of that class
• Difference b/w protected members and
private members
– The protected members of a base class are,
however, accessible by the members of its
derived classes but
– The private members of the base class are not
accessible directly by members of its derived
General syntax for defining a
derived class
class sub_class_name : specifier base_class_name
{
-----------
members of derived class
-----------
};
where
sub_class_name -> represents name of the derived
class
: (colon) -> sets relation b/w the classes
specifier -> represents the access specifier. It
may be public, private or
protected
base_class_name -> represents name of the base class
Types of inheritance
Single inheritance
Types of inheritance
• Public inheritance
• Private inheritance
• Protected inheritance
Public inheritance
• Public inheritance
– The public members of the base class
become the public members of the
derived class
– Thus the objects of the derived class can
only access public members (data and
functions) of the base class
– The protected members of the base
class also becomes protected members
of derived class or can access protected
data members of the base class
General syntax for deriving a
public class from base class
class sub_class_name : public base_class_name
{
-----------
-----------
-----------
};
where
public -> specifies the public
inheritance
sub_class_name -> represents name of the
derived class
base_class_name -> represents name of the base
class
An example of Public
•
•
1
2
inheritance
// public1.cpp
// An example of a public inheritance
• 3 #include <iostream>
• 4
• 5
• 6
• 7 class student
• 9 {
• 10 private:
• 11 char name[15], address[25]; //declare character arrays
• 12 public:
• 13 void input (void)
• 14 { cout<< “Enter Name “;
• 15 cin>>name;
• 16 cout <<“Enter Address “;
• 17 cin>>address;
• 18 }
• 19 void show (void)
• 20 { cout<<“\nName: “<<name<<endl;
• 21 cout<<“Address: “<<address<<endl;
• 22 } };
• 23 class marks: public student
• 24 {
• 25 private:
• 26 int s1,s2,s3,total;
• 27 public:
• 28 void inputmarks (void)
• 29 { cout<<“\nEnter marks of sub1 “; cin>>s1;
• 30 cout<<“Enter marks of sub2 “; cin>>s2;
• 31 cout<<“Enter marks of sub3 “; cin>>s3;
• 32 total= s1+s2+s3;
• 33 } void show_detail (void); };
An example of Public
•
•
34
35
inheritance
void main()
{
• 36 marks mmm;
• 37 mmm.input();
• 38 mmm.inputmarks();
• 39 mmm.show();
• 39 mmm.show_detail();
• 40 getch(); }
• 41 void marks :: show_detail() //defining member function “show_detail” outside
the class “marks”
• 42 {
• 43 cout<<“\nMarks of 1st sub: “<<s1<<endl; cout<<“Marks of 2st sub: “<<s2<<endl;
• 44 cout<<“Marks of 3rd sub: “<<s3<<endl;
• 45 cout<<“Total marks :”<<total<<endl;
• 46
• 48 } // end main
Enter Name shehzad
Enter Address hayatabad
Name: shehzad
Address: hayatabad
where
private -> specifies the private
inheritance
sub_class_name -> represents name of the
derived class
base_class_name -> represents name of the base
class
An example of Private
•
•
1
2
inheritance
// private1.cpp
// An example of a private inheritance
• 3 #include <iostream.h>
• 4
• 5
• 6 class A
• 7 {
• 8 private:
• 9 int a1,a2;
• 10 public:
• 11 void ddd() { cout<<“cannot access public member function”; }
• 12 protected:
• 13 int p1,p2;
• 14
• 15 void ppp(void) //protected member function
• 16 {
• 17 cout<<”Value of p1 of class A= “<<p1<<endl;
• 18 cout<<”Value of p2 of class A= “<<p2<<endl; } };
• 19 class B : private A
• 20 {
• 21 public:
• 22 void get(void)
• 23 {
• 24 cout<<“Enter value of p1 ? ”; cin>>p1;
• 25 cout<<“Enter value of p2 ? ”; cin>>p2;
• 26 ppp(); //call protected member function ppp()
• 27 } };
• 28 int main() //ppp() is not acessible in main because it is protected
• 29 {
• 30 B ob;
• 31 ob.get();
• 32 ob.ddd(); //ddd() is not accessiable
• 33 return 0; // indicates successful termination
• 34 } // end main
Enter value of p1 ?2
Enter value of p2 ?3
Value of p1 of class A=2
Value of p2 of class A=3
Private inheritance (con’t)
• In the previous program, the class “B” is
derived as private from the base class “A”
• The objects of the class “B”:
– Cannot access the private data members “a1”
& “a2” of base class “A”
– Cannot access the public member function
“ddd” of base class “A”
– Can access the protected data members “p1”
& “p2” of base class “A”
– Can access the protected member function
“ppp”
of base class “A”
Protected inheritance
• Protected inheritance
– The objects of the derived class that is
derived as protected can access only
the protected members of the base
class
General syntax for deriving a
protected class from base class
class sub_class_name : protected base_class_name
{
-----------
-----------
-----------
};
where
protected -> specifies the protected
inheritance
sub_class_name -> represents name of the
derived class
base_class_name -> represents name of the base
class
An example of Protected
•
•
1
2
inheritance
// protected1.cpp
// An example of a protected inheritance
• 3 #include <iostream.h>
• 4
• 5
• 6
• 7 class A
• 9 {
• 10 private:
• 11 int a1,a2;
• 12 protected:
• 13 int p1,p2;
• 14 public:
• 15 void ppp(void)
• 16 {
• 17 cout>>”Value of p1 of class A= “<<p1<<endl;
• 18 cout>>”Value of p2 of class A= “<<p2<<endl; } };
• 19 class B : protected A
• 20 {
• 21 public:
• 22 void get(void)
• 23 {
• 24 cout<<“Enter value of p1 ? ”; cin>>p1;
• 25 cout<<“Enter value of p2 ? ”; cin>>p2;
• 26 cout>>”Value of p1 of class A= “<<p1<<endl;
• 27 cout>>”Value of p2 of class A= “<<p2<<endl; } };
• 28 void main()
• 29 {
• 30 B ob;
• 31 ob.get();
• 32
• 33 return 0; // indicates successful termination
• 34 } // end main
Enter value of p1 ?2
Enter value of p2 ?3
Value of p1 of class A=2
Value of p2 of class A=3
Protected inheritance
(con’t)
• In the previous program, the class
“B” is derived as protected from the
base class “A”
• The objects of the class “B”:
– Can only access the protected data
members “p1” & “p2” of base class “A”
Types of inheritance
Multiple inheritance
Multiple inheritance
(con’t)
• In multiple inheritance, a class is derived
by using more than one classes
• In multiple inheritance, the derived class
receives the members of two or more base
classes
• Multiple inheritance is a powerful feature
of OOP
• This technique reduces the program size
and saves programmer’s time
• The programmer uses the existing base
classes in the program coding to solve
problems
Multiple Inheritance (con’t)
Member1 Member1 Member1
Member2 Member2 Member2
Member1
Member2
• In the figure, the arrow shows that the Derived class D can access the
members of base class A, B and C respectively, but the base classes
cannot access members of it derived class
General syntax for defining a
derived class from multiple
base classes
class sub_class : specifier base_class1, specifier
base_class2, …
{
-----------
members of derived class
-----------
};
where
sub_class_name -> represents name of the derived
class
: (colon) -> sets relation b/w the classes
specifier base_class1 -> represents the access specifier of
the first base class.
specifier base_class2 -> represents the access specifier of
the second base class.
An example of Multiple
•
•
1
2
inheritance
// multiple1.cpp
// An example of a multiple inheritance
• 3 #include <iostream.h>
• 4
• 5
• 6
• 7 class student
• 9 {
• 10 private:
• 11 char name[15], address[15]; //declare character arrays
• 12 public:
• 13 void input (void)
• 14 { cout<< “Enter name: “;
• 15 cin>>name;
• 16 cout <<“Enter Address: “;
• 17 cin>>address;
• 18 }
• 19 void print (void)
• 20 { cout<< “Name: “<<name<<endl;
• 21 cout <<“Address: “<<address<<endl;
• 22 };
• 23 class marks
• 24 {
• 25 private:
• 26 int s1,s2,s3,total;
• 27 public:
• 28 void inputmarks (void)
• 29 { cout<<“Enter marks of sub1: “; cin>>s1;
• 30 cout<<“Enter marks of sub2: “; cin>>s2;
• 31 cout<<“Enter marks of sub3: “; cin>>s3;
• 32 total= s1+s2+s3; }
• 33 void showmarks(void) {
• 34 cout<<“Marks of 1st sub: “<<s1<<endl; cout<<“Marks of 2st sub: “<<s2<<endl;
• 35 cout<<“Marks of 3rd sub: “<<s3<<endl;
• 36 cout<<“Total marks :”<<total<<endl;
• 37 }
• 38 };
• 39 class show : public student, public marks
• 40 {
• 41 public:
• 42 void show_rec(void)
• 43 {
• 44 cout<<“\nThe complete record is ”<<endl;
• 45 cout<<“==========================“<<endl;
• 46
• 47
• 48 }
• 49 };
• 50 int main()
• 51 {
• 52 show mo;
• 53 mo.input();
• 54 mo.inputmarks();
• 55 mo.show_rec();
• 56 mo.print();
• 57 mo.showmarks();
• 58 return 0; // indicates successful termination
• 59 } // end main
Enter name: shehzad
Enter Address: hayatabad
Enter marks of sub1:52
Enter marks of sub2:63
Enter marks of sub3:85