SlideShare a Scribd company logo
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

basics of c++
basics of c++basics of c++
basics of c++
gourav kottawar
 
Chapter Seven(2)
Chapter Seven(2)Chapter Seven(2)
Chapter Seven(2)bolovv
 
C Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.comC Programming Tutorial - www.infomtec.com
C Programming Tutorial - www.infomtec.com
M-TEC Computer Education
 
Managing console input
Managing console inputManaging console input
Managing console input
rajshreemuthiah
 
#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 algorithms
Nico Ludwig
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
Piotr Paradziński
 
Basic c++ programs
Basic c++ programsBasic c++ programs
Basic c++ programs
harman kaur
 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
Eelco 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_seo
JinTaek Seo
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Computer science sqp
Computer science sqpComputer science sqp
Computer science sqp
Prasanth566435
 
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 files
ramya marichamy
 
Managing console
Managing consoleManaging console
Managing console
Shiva Saxena
 
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 Technologies
Tushar 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 objetos
Sé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 notes
VIKAS 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 ppt
Shobhit 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

C++ language
C++ languageC++ language
C++ language
Hamza Asif
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
satvirsandhu9
 
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
 
Introduction to c++ ppt
Introduction to c++ pptIntroduction to c++ ppt
Introduction to c++ ppt
Prof. Dr. K. Adisesha
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
aptechsravan
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
Srichandan Sobhanayak
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
C++ Version 2
C++  Version 2C++  Version 2
C++ Version 2
JIGAR MAKHIJA
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Srikanth Mylapalli
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
Prof. Dr. K. Adisesha
 
Object oriented programming c++
Object oriented programming c++Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Lab 1.pptx
Lab 1.pptxLab 1.pptx
Lab 1.pptx
MohammedAlobaidy16
 
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
PushkarNiroula1
 
object oriented programming language fundamentals
object oriented programming language fundamentalsobject oriented programming language fundamentals
object oriented programming language fundamentals
ChaithraCSHirematt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
ssuser0c24d5
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
nilesh405711
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
YashpalYadav46
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
DevliNeeraj
 

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

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

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

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