KTU CS403 Programming Paradigms
Module - 5 Part 1
Data Abstraction & Encapsulation in �Object Oriented Programming
Abstraction- Introduction
Abstraction - Advantages
1. reduces the conceptual load for the programmer
2. provides a method of fault containment by preventing the programmer from using code in an inappropriate way
3. increases the independence among program components
Elements of object-oriented programming:�
Object oriented Languages
Fundamental OO Concepts
Encapsulation
Encapsulation in different languages
Abstraction Vs Encapsulation
Encapsulation-Example in C#
Encapsulation-Example in Java
KTU CS403 Programming Paradigms
Module - 5 Part 2
Inheritance
INHERITANCE
Inheritance
Inheritance terms
Syntax of Inheritance�
class push_button : public widget { ... }
public class push_button extends widget { ... }
type push_button is new widget with ...
Inheritance - Visibility in Derived Classes
class A : public B { ... }
class A : protected B { ... }
class A : private B { ... }
20
Class Access Specifiers-In C++
determines how private, protected, and public members of base class are inherited by the derived class
KTU CS403 Programming Paradigms
Module - 5 Part 3
Encapsulation
Encapsulation - concepts
Modules
Modules�
Modules
Classes�
Inheritance vs. Access
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
class Test : public Grade
When Test class inherits
from Grade class using
public class access, it looks like this:
private members:
int numQuestions:
float pointsEach;
int numMissed;
public members:
Test(int, int);
void setScore(float);
float getScore();
char getLetter();
Inheritance vs. Access
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
When Test class inherits
from Grade class using
protected class access, it looks like this:
private members:
int numQuestions:
float pointsEach;
int numMissed;
public members:
Test(int, int);
protected members:
void setScore(float);
float getScore();
float getLetter();
class Test : protected Grade
Inheritance vs. Access
private members:
int numQuestions:
float pointsEach;
int numMissed;
void setScore(float);
float getScore();
float getLetter();
public members:
Test(int, int);
private members:
char letter;
float score;
void calcGrade();
public members:
void setScore(float);
float getScore();
char getLetter();
class Grade
private members:
int numQuestions;
float pointsEach;
int numMissed;
public members:
Test(int, int);
When Test class inherits
from Grade class using
private class access, it looks like this:
class Test : private Grade
Visibility rules in other languages
Visibility rules in other languages
Classes-visibility
Classes-visibility
Inner Classes
Inner Classes In java
Inner class
Type Extension
Extending without Inheritance�
Extending without Inheritance�
KTU CS403 Programming Paradigms
Module - 5 Part 4
Polymorphism
Polymorphism-overriding
Method overloading
KTU CS403 Programming Paradigms
Module - 5 Part 5
CONSTRUCTORS & DESTRUCTORS
Constructors
Constructors
Constructor
51
52
Eiffel constructors and Expanded objects
!!var.creator(args)
Syntax
class CLASSNAME
{
public:
CLASSNAME([parameter list]);
};
class A
{
int x;
public:
A(); //Constructor
};
Example in C++
#include<iostream.h>
#include<conio.h>
class Rectangle
{
private:
int length , breadth;
public:
Rectangle()
{
Length=5,breadth=6;
}
int area()
{
int a=(length*breadth);
cout<<“area is”<<a;
}
};
void main()
{
Rectangle r1;
r1.area();
}
Types of constructors
In java
In C++
1.Default Constructor
2.Parameterized Constructor
3.Copy Constructor
Default Constructor�
class_name ()
{ Constructor Definition
}
Default Constructor Example�
class Cube
{
int side;
public:
Cube()
{
side=10;
}
};
int main()
{
Cube c;
cout << c.side;
}
Parameterized Constructor
Parameterized Constructor
class Cube
{
public:
int side;
Cube(int x)
{
side=x;
}
};
int main()
{
Cube c1(10);
Cube c2(20);
Cube c3(30);
cout << c1.side;
cout << c2.side;
cout << c3.side;
}
OUTPUT : 10 20 30
Copy constructor
Example
class counter
{
int c;
public:
counter(int a)
//single parameter constructor
{c=a;}
counter(counter &ob)
//copy constructor
{cout<<“copy constructor invoked”;
c=ob.c;
}
void show()
{cout<<c;
}};
void main()
{
counter c1(10);
counter c2(c1);// call copy constructor
c1.show();
c2.show();
}
Destructors
(1) the function ends
(2) the program ends
(3) a block containing temporary variables ends
(4) a delete operator is called
Destructor
Destructors
class CLASSNAME
{
……………….
public:
~CLASSNAME([parameter list]);
};
Example
#include<iostream.h>
#include<conio.h>
class counter
{
int id;
public:
counter(int i)
{
Id=I;
cout<<“contructor of object with id=”<<id;
}
~counter()
{
cout<<“destructor with id=”<<id;
}
};
void main()
{
counter c1(1);
counter c2(2);
counter c3(3);
cout<<“\n end of main”;
getch();
}
Output
constructor of object with id=1
constructor of object with id=2
constructor of object with id=3
End of main
destructor with id=3
destructor with id=2
destructor with id=1
KTU CS403 Programming Paradigms
Module - 5 Part 6
STATIC DATA MEMBERS
STATIC DATA MEMBERS
class CLASSNAME
{
…………
static datatype DATAMEMBER;
…………………..
};
datatype CLASSNAME::DATAMEMBER=initial value ;
STATIC DATA MEMBERS
71
STATIC BINDING
72
DYNAMIC BINDING
class Human{
public static void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public static void eat(){
System.out.println("Boy is eating");
}
}
STATIC BINDING IN JAVA
public class Methover{
public static void main(String args[])
{
Boy obj = new Boy();
Human ob=new Human();
Human ob1=new Boy();
obj.eat();
ob.eat();
ob1.eat();
}
} Boy is eating
Human is eating
Human is eating
DYNAMIC BINDING IN JAVA
class Human
{
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human{
//Overriding method
public void eat(){
System.out.println("Boy is eating");
}
}
public class Methover{
public static void main(String args[])
{
Boy obj = new Boy();
Human ob=new Human();
Human ob1=new Boy();
obj.eat();
ob.eat();
ob1.eat();
}
}
Boy is eating
Human is eating
Boy is eating
Dynamic Method Binding
Dynamic Method Binding
77
Static Vs Dynamic binding
Dynamic Method Binding
student s;
professor p;
...
person * x = &s;
person * y = &p;
x->print_mailing_label (); // ??
y->print_mailing_label (); // ??
Dynamic binding in C++?�
Virtual and Non-Virtual Methods
Dynamic Method Binding in C++
All objects of a given class share the same vtable.
Abstract Classes
class person
{
...
public:
virtual void print_mailing_label () = 0;
...
};
class with no other members than abstract methods
Abstract Classes
C++
class person {
...
virtual void print_mailing_label() = 0;
...
};
Java
class person {
...
abstract void print_mailing_label();
...
};
class Vehicle {
public void start() {
System.out.println("Inside start method of Vehicle");
}}
class Car extends Vehicle {
@Override
public void start() {
System.out.println("Inside start method of Car");
}}
public class DynamicBindingTest {
public static void main(String args[]) {
Vehicle vehicle = new Car(); //here Type is vehicle but object will be Car
vehicle.start(); //Car's start called because start() is overridden method
}
}
//Output: Inside start method of Car
KTU CS403 Programming Paradigms
Module - 5 Part 7
MULTIPLE INHERITANCE
Multiple Inheritance
allows a derived class to have multiple base classes:
class A : public B, public C { ... }
base 1
base 2
derived
Multiple Inheritance
Multiple Inheritance
class cube : public square,public rectSolid;
class
square
class
rectSolid
class
cube
#include<iostream.h>
#include<conio.h>
class student {
protected:
int rno, m1, m2;
public:
void get() {
cout << "Enter the Roll no :";
cin>>rno;
cout << "Enter the two marks :";
cin >> m1>>m2;
}};
class sports {
protected:
int sm; // sm = Sports mark
public:
void getsm() {
cout << "\nEnter the sports mark :";
cin>>sm;
}};
class result : public student, public sports {
int tot, avg;
public:
void display() {
tot = (m1 + m2 + sm);
avg = tot / 3;
cout << "\n\n\tRoll No : " << rno << "\n\tTotal : " << tot;
cout << "\n\tAverage : " << avg;
}
};
void main() {
result obj;
obj.get();
obj.getsm();
obj.display();
}
Potential Problem
Base
Base
Derived 1
Derived 2
Derived 3
92
Virtual Base Class
Virtual Function
Pure Virtual Functions
virtual type func-name(paremeter-list) = 0
KTU CS403 Programming Paradigms
Module - 5 Part 8
Innovative features of Scripting Languages
Scoping Rules
Scripting Languages
Types of Scripting Languages
Types of Scripting Languages
Different Scripting Languages
Different Scripting Languages
Different Scripting Languages
Different Scripting Languages
Glue Languages
Scripting Language Evolution
Scripting Language Evolution
Scripting Language Evolution
Characteristics of scripting languages:
1.Both batch and interactive use
2.Economy of expression
3. Lack of declarations; simple scoping rules
4. Flexible dynamic typing
$a = “4”
print $a . 3 . “\n”
print $a + 3 . “\n”
Outputs the following:
43
7
5. Easy access to other programs
6. Sophisticated pattern matching and string manipulation
7. High level data types
Innovative Features: Scope and Names
Innovative Features: Scope and Names
Innovative Features: Scope and Names
i=1; j=3
def outer()
Innovative Features: Scope and Names
i <- 4
i <<- 4
KTU CS403 Programming Paradigms
Module - 5 Part 9
Innovative features of Scripting Languages
Strings and Pattern Matching
Innovative Features : String& Pattern matching
Innovative Features : String& Pattern matching
Pattern matching-POSIX RE
/ab(cd|ef)g*/ - Matches abcd, abcdg, abefg, abefgg, etc
Pattern matching-Extended RE
$_ = “albatross”;
if (/ba.*s+/) … #true
if (/^ba.*s+/) … #false - no match at start
$foo = “albatross”;
$foo =~ s/lbat/c; #now across
Regular expression escape sequences in Perl
Pattern matching-Greedy and Minimal Matches
Pattern matching-Greedy Matches
Implicit capture of prefix,match, and suffix
KTU CS403 Programming Paradigms
Module - 5 Part 10
Innovative features of Scripting Languages
Data Types & Objects orientation
Innovative Features: Data Types
Innovative Features: Data Types-Numeric Types�
Innovative Features: Data Types
If we type the following in Ruby,
a = "4"
print a + 3, "\n"
we get the following message at run time: “In ‘+’: failed to convert Fixnum intoString (TypeError).”�In Perl is much more forgiving. �$a = "4";
print $a . 3 . "\n"; # ’.’ is concatenation
print $a + 3 . "\n"; # ’+’ is addition
//prints 43 and 7.
Innovative Features: Data Types
a = [] # empty array assignment
a[3] = "1"
Innovative Features: Data Types-Composite Types�
$foo is a scalar � @foo is an array;� %foo is a hash;� &foo is a subroutine; and� Plain foo is a filehandle or an I/O format , Depending on context
Innovative Features: Data Types-Composite Types�
crimson = (0xdc, 0x14, 0x3c) # R,G,B components
a, b = b, a # swap
Innovative Features: Data Types-Composite Types�
Innovative Features: Data Types-Composite Types�
Innovative Features: Data Types-Composite Types�
double d = 3;
Innovative Features: Data Types-Composite Types�
$time = gmtime();
@time_arry = gmtime();
Innovative Features: Object Orientation�
Innovative Features: Object Orientation�