SlideShare a Scribd company logo
1 of 32
Download to read offline
Programming Language Concepts 
Lecture 18 
Prepared by 
Manuel E. Bermúdez, Ph.D. 
Associate Professor 
University of Florida 
Object-Oriented Programming Part 1
Object Oriented Programming 
•Over time, data abstraction has become essential as programs became complicated. 
•Benefits: 
1. Reduce conceptual load (minimum detail). 
2. Fault containment. 
3. Independent program components. 
(difficult in practice). 
•Code reuse possible by extending and refining abstractions.
Object Oriented Programming 
•A methodology of programming 
•Four (Five ?) major principles: 
1.Data Abstraction. 
2.Encapsulation. 
3.Information Hiding. 
4.Polymorphism (dynamic binding). 
5.Inheritance. (particular case of polymorphism ?) 
Will describe these using C++, because ...
The C++ language 
•An object-oriented, general-purpose programming language, derived from C (C++ = C plus classes). 
•C++ adds the following to C: 
1.Inlining and overloading of functions. 
2.Default argument values. 
3.Argument pass-by-reference. 
4.Free store operators new and delete, instead of malloc() and free(). 
5.Support for object-oriented programming, through classes, information hiding, public interfaces, operator overloading, inheritance, and templates.
Design Objectives in C++ 
•Compatibility. Existing code in C can be used. Even existing, pre-compiled libraries can be linked with new C++ code. 
•Efficiency. No additional cost for using C++. Overheadof function calls is eliminated where possible. 
•Strict type checking. Aids debugging, allows generation of efficient code. 
•C++ designed by Bjarne Stroustrup of Bell Labs (now at TAMU). 
•Standardization: ANSI, ISO.
Non Object-Oriented Extensions to C 
•Major improvements over C. 
1.Stream I/O. 
2.Strong typing. 
3.Parameter passing by reference. 
4.Default argument values. 
5.Inlining. 
We’ve discussed some of these already.
Stream I/O in C++ 
•Input and output in C++ is handled by streams. 
•The directive #include <iostream.h> declares 2 streams: cin and cout. 
• cin is associated with standard input. Extraction: operator>>. 
• cout is associated with standard output. Insertion: operator<<. 
•In C++, input is line buffered, i.e. the user must press <RTN> before any characters are processed.
Example of Stream I/O in C++ 
A function that returns the sum of 
the numbers in the file Number.in 
int fileSum(); 
{ 
ifstream infile("Number.in"); 
int sum = 0; 
int value; 
//read until non-integer or <eof> 
while(infile >> value) 
sum = sum + value; 
return sum; 
}
Example of Stream I/O in C++ 
Example 2: A function to copy myfile into copy.myfile 
void copyfile() 
{ 
ifstream source("myfile"); 
ofstream destin("copy.myfile"); 
char ch; 
while (source.get(ch)) 
destin<<ch; 
}
Line-by-line textfile concatenation 
int ch; 
// Name1, Name2, Name3 are strings 
ifstream f1 (Name1); 
ifstream f2 (Name2); 
ofstream f3 (Name3); 
while ((ch = f1.get())!=-1 ) 
if (ch =='n') 
while ((ch = f2.get())!=-1) { 
f3.put(ch); 
if (ch == 'n') break; 
} 
else f3.put(ch); 
}
Why use I/O streams ? 
•Streams are type safe -- the type of object being I/O'd is known statically by the compiler rather than via dynamically tested '%' fields. 
•Streams are less error prone: 
– Difficult to make robust code using printf. 
•Streams are faster: printf interprets the language of '%' specs, and chooses (at runtime) the proper low-level routine. C++ picks these routines statically based on the actual types of the arguments.
Why use I/O streams ? (cont’d) 
•Streams are extensible -- the C++ I/O mechanism is extensible to new user-defined data types. 
•Streams are subclassable -- ostream and istream (C++ replacements for FILE*) are real classes, and hence subclassable. Can define types that look and act like streams, yet operate on other objects. Examples: 
–A stream that writes to a memory area. 
–A stream that listens to external port.
C++ Strong Typing 
•There are 6 principal situations in which C++ has stronger typing than C. 
1.The empty list of formal parameters means "no arguments" in C++. 
•In C, it means "zero or more arguments", with no type checking at all. Example: 
char * malloc();
C++ Strong Typing (cont’d) 
2.In C, it's OK to use an undefined function; no type checking will be performed. In C++, undefined functions are not allowed. 
Example: 
main() 
f( 3.1415 ); 
// C++: error, f not defined 
// C: OK, taken to mean int f()
C++ Strong Typing (cont’d) 
3.A C function, declared to be value- returning, can fail to return a value. Not in C++. Example: 
double foo() { 
/* ... */ 
return; 
} 
main() { 
if ( foo() ) { ... } 
... 
} 
// C : OK 
// C++: error, no return value.
C++ Strong Typing (cont’d) 
4.In C, assigning a pointer of type void* to a pointer of another type is OK. Not in C++. Example: 
int i = 1024; 
void *pv = &i; 
// C++: error, 
// explicit cast required. 
// C : OK. 
char *pc = pv; 
int len = strlen(pc);
C++ Strong Typing (cont’d) 
5.C++ is more careful about initializing arrays: Example: 
char A[2]="hi"; 
// C++: error, 
// not enough space for '0' 
// C : OK, but no '0' is stored. 
It's best to stick with char A[] = "hi“;
C++ Strong Typing (cont’d) 
6.Free store (heap) management. In C++, we use new and delete, instead of malloc and free. 
•malloc() doesn't call constructors, and free doesn't call destructors. 
•new and delete are type safe.
Object-Oriented Programming 
Object-oriented programming is a programming 
methodology characterized by the following concepts: 
1.Data Abstraction: problem solving via the formulation of abstract data types (ADT's). 
2.Encapsulation: the proximity of data definitions and operation definitions. 
3.Information hiding: the ability to selectively hide implementation details of a given ADT. 
4.Polymorphism: the ability to manipulate different kinds of objects, with only one operation. 
5.Inheritance: the ability of objects of one data type, to inherit operations and data from another data type. Embodies the "is a" notion: a horse is a mammal, a mammal is a vertebrate, a vertebrate is a lifeform.
O-O Principles and C++ Constructs 
O-O Concept C++ Construct(s) 
Abstraction Classes 
Encapsulation Classes 
Information Hiding Public and Private Members 
Polymorphism Operator overloading, 
templates, virtual functions 
Inheritance Derived Classes
O-O is a different Paradigm 
•Central questions when programming. 
–Imperative Paradigm: 
–What to do next ? 
–Object-Oriented Programming 
–What does the object do ? (vs. how) 
•Central activity of programming: 
–Imperative Paradigm: 
–Get the computer to do something. 
–Object-Oriented Programming 
–Get the object to do something.
C vs. C++, side-by-side
C vs. C++, side-by-side (cont’d) 
In C++, methods can appear inside the class definition 
(better encapsulation)
C vs. C++, side-by-side (cont’d) 
In C++, no explicit referencing. 
Could have overloaded <<, >> for Stacks: 
s << 1; s >> i;
Structures and Classes in C++ 
•Structures in C++ differ from those in C in that members can be functions. 
•A special member function is the “constructor”, whose name is the same as the structure. It is used to initialize the object: 
struct buffer { 
buffer() 
{size=MAXBUF+1; front=rear=0;} 
char buf[MAXBUF+1]; 
int size, front, rear; 
}
Structures and Classes in C++ 
The idea is to add some operations on objects 
of type buffer: 
struct buffer { 
buffer() {size=MAXBUF+1;front=rear=0;} 
char buf[MAXBUF+1]; 
int size, front, rear; 
int succ(int i) {return (i+1)%size;} 
int enter(char); 
char leave(); 
}
Structures and Classes in C++ 
The definition (body) of a member function can be 
included in the structure's declaration, or may 
appear later. If so, use the name resolution 
operator (::) 
int buffer::enter(char x) { 
// body of enter } 
char buffer::leave() { 
// body of leave }
Public and Private Members 
Structures and classes are closely related in C++: 
struct x { <member-dclns> }; 
is equivalent to 
class x { public: <member-dclns>}; 
Difference: by default, members of a structure are 
public; members of a class are private. So, 
class x { <member-dclns> }; 
is the same as 
struct x { private: <member-dclns> };
Header File Partitioning
Header File Partitioning (cont’d)
Header File Partitioning (cont’d)
Programming Language Concepts Lecture 18 
Prepared by 
Manuel E. Bermúdez, Ph.D. 
Associate Professor 
University of Florida 
Object-Oriented Programming Part 1

More Related Content

What's hot

Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)bolovv
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class StructureHadziq Fabroyir
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithmsNico Ludwig
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programsharman kaur
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementEelco Visser
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seoJinTaek Seo
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++Haresh Jaiswal
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with filesramya marichamy
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Piotr Paradziński
 
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...Kamiya Toshihiro
 

What's hot (20)

basics of c++
basics of c++basics of c++
basics of c++
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
 
Managing console input
Managing console inputManaging console input
Managing console input
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure#OOP_D_ITS - 4th - C++ Oop And Class Structure
#OOP_D_ITS - 4th - C++ Oop And Class Structure
 
(2) collections algorithms
(2) collections algorithms(2) collections algorithms
(2) collections algorithms
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Computer science sqp
Computer science sqpComputer science sqp
Computer science sqp
 
01. introduction to C++
01. introduction to C++01. introduction to C++
01. introduction to C++
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing console
Managing consoleManaging console
Managing console
 
Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...Big picture of category theory in scala with deep dive into contravariant and...
Big picture of category theory in scala with deep dive into contravariant and...
 
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
An Execution-Semantic and Content-and-Context-Based Code-Clone Detection and ...
 

Viewers also liked

Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesTushar B Kute
 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
 
Conceitos básicos de orientação a objetos
Conceitos básicos de orientação a objetosConceitos básicos de orientação a objetos
Conceitos básicos de orientação a objetosSérgio Souza Costa
 
Polymorphism (Ad-hoc and Universal)
Polymorphism (Ad-hoc and Universal)Polymorphism (Ad-hoc and Universal)
Polymorphism (Ad-hoc and Universal)Sérgio Souza Costa
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notesVIKAS SINGH BHADOURIA
 
System concepts, elements and types of systems ppt
System concepts, elements and types of systems pptSystem concepts, elements and types of systems ppt
System concepts, elements and types of systems pptShobhit Sharma
 

Viewers also liked (7)

Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
 
Conceitos básicos de orientação a objetos
Conceitos básicos de orientação a objetosConceitos básicos de orientação a objetos
Conceitos básicos de orientação a objetos
 
Polymorphism (Ad-hoc and Universal)
Polymorphism (Ad-hoc and Universal)Polymorphism (Ad-hoc and Universal)
Polymorphism (Ad-hoc and Universal)
 
Principles of programming languages. Detail notes
Principles of programming languages. Detail notesPrinciples of programming languages. Detail notes
Principles of programming languages. Detail notes
 
System concepts, elements and types of systems ppt
System concepts, elements and types of systems pptSystem concepts, elements and types of systems ppt
System concepts, elements and types of systems ppt
 
System concepts
System conceptsSystem concepts
System concepts
 

Similar to L6

Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Languagesatvirsandhu9
 
The Differences Between C and C++
The Differences Between C and C++The Differences Between C and C++
The Differences Between C and C++LingarajSenapati2
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started CppLong Cao
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++Ankur Pandey
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itPushkarNiroula1
 
object oriented programming language fundamentals
object oriented programming language fundamentalsobject oriented programming language fundamentals
object oriented programming language fundamentalsChaithraCSHirematt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.pptDevliNeeraj
 

Similar to L6 (20)

C++ language
C++ languageC++ language
C++ language
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
The Differences Between C and C++
The Differences Between C and C++The Differences Between C and C++
The Differences Between C and C++
 
Getting Started Cpp
Getting Started CppGetting Started Cpp
Getting Started Cpp
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
 
Introduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to itIntroduction to cpp language and all the required information relating to it
Introduction to cpp language and all the required information relating to it
 
object oriented programming language fundamentals
object oriented programming language fundamentalsobject oriented programming language fundamentals
object oriented programming language fundamentals
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 

More from lksoo

More from lksoo (20)

Lo48
Lo48Lo48
Lo48
 
Lo43
Lo43Lo43
Lo43
 
Lo39
Lo39Lo39
Lo39
 
Lo37
Lo37Lo37
Lo37
 
Lo27
Lo27Lo27
Lo27
 
Lo17
Lo17Lo17
Lo17
 
Lo12
Lo12Lo12
Lo12
 
T3
T3T3
T3
 
T2
T2T2
T2
 
T1
T1T1
T1
 
T4
T4T4
T4
 
P5
P5P5
P5
 
P4
P4P4
P4
 
P3
P3P3
P3
 
P1
P1P1
P1
 
P2
P2P2
P2
 
L10
L10L10
L10
 
L9
L9L9
L9
 
L8
L8L8
L8
 
L7
L7L7
L7
 

Recently uploaded

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 

Recently uploaded (20)

History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 

L6

  • 1. Programming Language Concepts Lecture 18 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming Part 1
  • 2. Object Oriented Programming •Over time, data abstraction has become essential as programs became complicated. •Benefits: 1. Reduce conceptual load (minimum detail). 2. Fault containment. 3. Independent program components. (difficult in practice). •Code reuse possible by extending and refining abstractions.
  • 3. Object Oriented Programming •A methodology of programming •Four (Five ?) major principles: 1.Data Abstraction. 2.Encapsulation. 3.Information Hiding. 4.Polymorphism (dynamic binding). 5.Inheritance. (particular case of polymorphism ?) Will describe these using C++, because ...
  • 4. The C++ language •An object-oriented, general-purpose programming language, derived from C (C++ = C plus classes). •C++ adds the following to C: 1.Inlining and overloading of functions. 2.Default argument values. 3.Argument pass-by-reference. 4.Free store operators new and delete, instead of malloc() and free(). 5.Support for object-oriented programming, through classes, information hiding, public interfaces, operator overloading, inheritance, and templates.
  • 5. Design Objectives in C++ •Compatibility. Existing code in C can be used. Even existing, pre-compiled libraries can be linked with new C++ code. •Efficiency. No additional cost for using C++. Overheadof function calls is eliminated where possible. •Strict type checking. Aids debugging, allows generation of efficient code. •C++ designed by Bjarne Stroustrup of Bell Labs (now at TAMU). •Standardization: ANSI, ISO.
  • 6. Non Object-Oriented Extensions to C •Major improvements over C. 1.Stream I/O. 2.Strong typing. 3.Parameter passing by reference. 4.Default argument values. 5.Inlining. We’ve discussed some of these already.
  • 7. Stream I/O in C++ •Input and output in C++ is handled by streams. •The directive #include <iostream.h> declares 2 streams: cin and cout. • cin is associated with standard input. Extraction: operator>>. • cout is associated with standard output. Insertion: operator<<. •In C++, input is line buffered, i.e. the user must press <RTN> before any characters are processed.
  • 8. Example of Stream I/O in C++ A function that returns the sum of the numbers in the file Number.in int fileSum(); { ifstream infile("Number.in"); int sum = 0; int value; //read until non-integer or <eof> while(infile >> value) sum = sum + value; return sum; }
  • 9. Example of Stream I/O in C++ Example 2: A function to copy myfile into copy.myfile void copyfile() { ifstream source("myfile"); ofstream destin("copy.myfile"); char ch; while (source.get(ch)) destin<<ch; }
  • 10. Line-by-line textfile concatenation int ch; // Name1, Name2, Name3 are strings ifstream f1 (Name1); ifstream f2 (Name2); ofstream f3 (Name3); while ((ch = f1.get())!=-1 ) if (ch =='n') while ((ch = f2.get())!=-1) { f3.put(ch); if (ch == 'n') break; } else f3.put(ch); }
  • 11. Why use I/O streams ? •Streams are type safe -- the type of object being I/O'd is known statically by the compiler rather than via dynamically tested '%' fields. •Streams are less error prone: – Difficult to make robust code using printf. •Streams are faster: printf interprets the language of '%' specs, and chooses (at runtime) the proper low-level routine. C++ picks these routines statically based on the actual types of the arguments.
  • 12. Why use I/O streams ? (cont’d) •Streams are extensible -- the C++ I/O mechanism is extensible to new user-defined data types. •Streams are subclassable -- ostream and istream (C++ replacements for FILE*) are real classes, and hence subclassable. Can define types that look and act like streams, yet operate on other objects. Examples: –A stream that writes to a memory area. –A stream that listens to external port.
  • 13. C++ Strong Typing •There are 6 principal situations in which C++ has stronger typing than C. 1.The empty list of formal parameters means "no arguments" in C++. •In C, it means "zero or more arguments", with no type checking at all. Example: char * malloc();
  • 14. C++ Strong Typing (cont’d) 2.In C, it's OK to use an undefined function; no type checking will be performed. In C++, undefined functions are not allowed. Example: main() f( 3.1415 ); // C++: error, f not defined // C: OK, taken to mean int f()
  • 15. C++ Strong Typing (cont’d) 3.A C function, declared to be value- returning, can fail to return a value. Not in C++. Example: double foo() { /* ... */ return; } main() { if ( foo() ) { ... } ... } // C : OK // C++: error, no return value.
  • 16. C++ Strong Typing (cont’d) 4.In C, assigning a pointer of type void* to a pointer of another type is OK. Not in C++. Example: int i = 1024; void *pv = &i; // C++: error, // explicit cast required. // C : OK. char *pc = pv; int len = strlen(pc);
  • 17. C++ Strong Typing (cont’d) 5.C++ is more careful about initializing arrays: Example: char A[2]="hi"; // C++: error, // not enough space for '0' // C : OK, but no '0' is stored. It's best to stick with char A[] = "hi“;
  • 18. C++ Strong Typing (cont’d) 6.Free store (heap) management. In C++, we use new and delete, instead of malloc and free. •malloc() doesn't call constructors, and free doesn't call destructors. •new and delete are type safe.
  • 19. Object-Oriented Programming Object-oriented programming is a programming methodology characterized by the following concepts: 1.Data Abstraction: problem solving via the formulation of abstract data types (ADT's). 2.Encapsulation: the proximity of data definitions and operation definitions. 3.Information hiding: the ability to selectively hide implementation details of a given ADT. 4.Polymorphism: the ability to manipulate different kinds of objects, with only one operation. 5.Inheritance: the ability of objects of one data type, to inherit operations and data from another data type. Embodies the "is a" notion: a horse is a mammal, a mammal is a vertebrate, a vertebrate is a lifeform.
  • 20. O-O Principles and C++ Constructs O-O Concept C++ Construct(s) Abstraction Classes Encapsulation Classes Information Hiding Public and Private Members Polymorphism Operator overloading, templates, virtual functions Inheritance Derived Classes
  • 21. O-O is a different Paradigm •Central questions when programming. –Imperative Paradigm: –What to do next ? –Object-Oriented Programming –What does the object do ? (vs. how) •Central activity of programming: –Imperative Paradigm: –Get the computer to do something. –Object-Oriented Programming –Get the object to do something.
  • 22. C vs. C++, side-by-side
  • 23. C vs. C++, side-by-side (cont’d) In C++, methods can appear inside the class definition (better encapsulation)
  • 24. C vs. C++, side-by-side (cont’d) In C++, no explicit referencing. Could have overloaded <<, >> for Stacks: s << 1; s >> i;
  • 25. Structures and Classes in C++ •Structures in C++ differ from those in C in that members can be functions. •A special member function is the “constructor”, whose name is the same as the structure. It is used to initialize the object: struct buffer { buffer() {size=MAXBUF+1; front=rear=0;} char buf[MAXBUF+1]; int size, front, rear; }
  • 26. Structures and Classes in C++ The idea is to add some operations on objects of type buffer: struct buffer { buffer() {size=MAXBUF+1;front=rear=0;} char buf[MAXBUF+1]; int size, front, rear; int succ(int i) {return (i+1)%size;} int enter(char); char leave(); }
  • 27. Structures and Classes in C++ The definition (body) of a member function can be included in the structure's declaration, or may appear later. If so, use the name resolution operator (::) int buffer::enter(char x) { // body of enter } char buffer::leave() { // body of leave }
  • 28. Public and Private Members Structures and classes are closely related in C++: struct x { <member-dclns> }; is equivalent to class x { public: <member-dclns>}; Difference: by default, members of a structure are public; members of a class are private. So, class x { <member-dclns> }; is the same as struct x { private: <member-dclns> };
  • 32. Programming Language Concepts Lecture 18 Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Object-Oriented Programming Part 1