SlideShare a Scribd company logo
OBJECT ORIENTATION
FUNDAMENTALS
Michael Heron
Introduction
• In this lecture we are going to look at the structure and
syntax of an object oriented program in C++.
• In theory, it is the same as in Java.
• We shall also cover briefly the things that you should
remember from your previous exposure to the idea of
OOP.
• We’ll go into it all in much more detail as the course progresses.
Reminder of Concepts
• The basic building blocks of an Object Oriented program
are classes and objects.
• Classes define a blueprint, or structure
• Objects define the state.
• An object is an instantiation of a class.
• The class tells the object what structure and data fields it should
have.
• The object contains the value of those data fields.
Reminder of Concepts
• An Object Oriented program combined objects together to
achieve some total goal.
• Each object represents one part of a subdivision of labour.
• You can think of a class as a complex, user-defined data
type.
• You can create multiple objects from a single class.
• Usually
OOP Good Practise
• Methods and Attributes in an Object Oriented program
have degrees of visiboility.
• Variables should almost always be private.
• They are modified through public accessor methods that you define.
• Methods should have the level of visibility that minimises the
impact of change associated with them.
• More on this later, it’s an important concept.
Declaring A Class (Java)
public class Car {
private double price;
private String colour;
public void setPrice (double p) {
price = p;
}
public double getPrice() {
return price;
}
public void setColour (String c) {
colour = c;
}
public String getColour() {
return colour;
}
}
Declaring a Class (C++)
#include <iostream>
using namespace std;
class Car {
private:
float price;
string colour;
public:
void set_price (float p);
float query_price();
void set_colour (string c);
string query_colour();
};
Differences
• Visibility declared in groups in C++
• Defined at the method/variable level in Java
• Class declaration ends with a semi-colon in C++
• No such syntactic requirement in Java
• Difference in naming conventions.
• Not enforced by the compiler, but an adopted stylistic convention.
• Code definitions separated from code declarations.
A C++ Class
• A C++ class usually exists in two files.
• The header file defining the class declaration.
• That’s what we just say.
• The code file defining the method bodies.
• In Visual Studio, the wizards will handle this separation for
you.
• Other IDEs will most likely not.
• C++ uses the scope resolution operator (::) to define
code as belonging to a member function of a class.
Car Code File
#include "car.h"
void Car::set_price (float p) {
price = p;
}
float Car::query_price() {
return price;
}
void Car::set_colour (string c) {
colour = c;
}
string Car::query_colour() {
return colour;
}
Instantiating an Object in C++
• In Java, this was done using the new keyword.
• In C++, we also use new, but we also must declare a
pointer to the object we are about to create:
• When we have a pointer to an object, we use a ->
operator to access defined methods.
• Variables too, but don’t do that.
• This is analagous to the dot operator in Java.
• Be careful – C++ also has a dot operator.
• It doesn’t work work with pointers.
Using A Class
#include <iostream>
#include "car.h”
int main() {
Car *my_car;
my_car = new Car();
my_car->set_price (100.0);
cout << my_car->query_price() << endl;
return 1;
}
Instantiating An Object (2)
• You can also instantiate objects without using pointer notation.
• You access the methods and variables using the dot notation, just as
in Java.
• You don’t use the new keyword, you just declare the variable.
• The object then exists on the stack.
• When working with this object though, you will be working with
the value of it.
• This is somewhat limiting, as you cannot make changes to the state of
object outside of its context.
Instantiating An Object (2)
#include <iostream>
#include "car.h"
void set_value (Car, float);
int main() {
Car my_other_car;
my_other_car.set_colour ("blue");
set_value (my_other_car, 100.0);
cout << my_other_car.query_colour() << endl;
cout << my_other_car.query_price() << endl;
return 1;
}
void set_value (Car myCar, float val) {
myCar.set_price (val);
}
Classes and Pointers
• The most flexible way to work with objects in C++ (and
the way that will most closely map onto your experience
with objects in Java) is through pointers.
• The output for the program shown the slide before would
be:
blue
0.0
Classes and Pointers
#include <iostream>
#include "car.h"
void set_value (Car*, float);
int main() {
Car* my_car;
my_car = new Car();
my_car->set_colour ("blue");
set_value (my_car, 100.0);
cout << my_car->query_colour() << endl;
cout << my_car->query_price() << endl;
return 1;
}
void set_value (Car *myCar, float val) {
myCar->set_price (val);
}
Java Memory Management
• One of the features of java is the garbage collector.
• Essentially it does all the cleaning up of objects that are no longer in
use in a program.
• It means you don’t need to explicitly destroy objects once you are
done with them.
• The garbage collector will get to them eventually.
• Usually
• C++ requires you to destroy pointers to objects when you are
done with them.
• Or they stay in memory forever.
• Until the program finishes execution.
C++ Memory Management
• Whenever you are completely finished with an object in
C++, you must delete it.
• This frees up the memory that it had previously been assigned.
• From that point on, the memory is null memory.
• You’ll get an error message if you try to access it.
• The delete keyword is used to free up memory:
delete my_car;
Pointers and Objects
• Those of you who are especially observant may have noticed
some differences in the way we work with pointers when
dealing with primitive data types and with objects.
• There’s no inconsistency really, it’s just a consequence of how they are
defined.
• Remember what our two pointer operators actually do.
• * means ‘the value of’
• & means ‘the address of’
Pointers in Objects
• When we declare a class as a pointer, we are saying ‘we are
working with the value of this memory location’
• When we pass it into the function, we are already working with
a pointer.
• Thus we do not need to use the & operator when passing the
parameter.
• Within the function, we can use the variable name without the *
operator.
• The -> operator works directly on sections of memory.
Pointers in C++
• This can be tremendously confusing.
• It does become clear as time goes by, but expect this to be one of
the hurdles you must overcome to be a confident C++ programmer.
• As a result of this, the tutorial exercises tomorrow are on
pointers and pointer manipulation.
• Wear your thinkin’ caps!
Summary
• In the abstract, C++ classes work just like Java classes.
• There are syntactical and structural differences.
• The issue is further complicated by C++’s reliance on
pointers.
• Very powerful, but very confusing to begin with.
• Remember that Java does a fair degree of ‘administration’
for you.
• C++ is not quite so thoughtful!
Ad

More Related Content

What's hot (19)

Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
Cate Huston
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
Salahaddin University-Erbil
 
pebble - Building apps on pebble
pebble - Building apps on pebblepebble - Building apps on pebble
pebble - Building apps on pebble
Aniruddha Chakrabarti
 
A350103
A350103A350103
A350103
aijbm
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
Vishnu Shaji
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 
Of Lambdas and LINQ
Of Lambdas and LINQOf Lambdas and LINQ
Of Lambdas and LINQ
BlackRabbitCoder
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
NLJUG
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
Volodymyr Byno
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
C++ classes
C++ classesC++ classes
C++ classes
imhammadali
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
Martin Odersky
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
BlackRabbitCoder
 
Java Building Blocks
Java Building BlocksJava Building Blocks
Java Building Blocks
Cate Huston
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Effective Scala: Programming Patterns
Effective Scala: Programming PatternsEffective Scala: Programming Patterns
Effective Scala: Programming Patterns
Vasil Remeniuk
 
A350103
A350103A350103
A350103
aijbm
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Object Oriented Programming With C++
Object Oriented Programming With C++Object Oriented Programming With C++
Object Oriented Programming With C++
Vishnu Shaji
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
M Hussnain Ali
 
Real-World Scala Design Patterns
Real-World Scala Design PatternsReal-World Scala Design Patterns
Real-World Scala Design Patterns
NLJUG
 
Metaprogramming in Ruby
Metaprogramming in RubyMetaprogramming in Ruby
Metaprogramming in Ruby
Volodymyr Byno
 
Learn Concept of Class and Object in C# Part 3
Learn Concept of Class and Object in C#  Part 3Learn Concept of Class and Object in C#  Part 3
Learn Concept of Class and Object in C# Part 3
C# Learning Classes
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming LanguageSwift Tutorial Part 1. The Complete Guide For Swift Programming Language
Swift Tutorial Part 1. The Complete Guide For Swift Programming Language
Hossam Ghareeb
 
More Little Wonders of C#/.NET
More Little Wonders of C#/.NETMore Little Wonders of C#/.NET
More Little Wonders of C#/.NET
BlackRabbitCoder
 

Viewers also liked (6)

Long Term Care Insurance
Long Term Care InsuranceLong Term Care Insurance
Long Term Care Insurance
jlucas0214
 
Frsa flash 17 may
Frsa flash 17 mayFrsa flash 17 may
Frsa flash 17 may
Frsa Cavalry
 
Overview 1.2
Overview 1.2Overview 1.2
Overview 1.2
poetawan
 
Team leader
Team leaderTeam leader
Team leader
annakatkowska
 
Website intro 280910
Website intro 280910Website intro 280910
Website intro 280910
Khoo Christie
 
Southland Power Summary.Mar 09
Southland Power Summary.Mar 09Southland Power Summary.Mar 09
Southland Power Summary.Mar 09
guestd2111a
 
Long Term Care Insurance
Long Term Care InsuranceLong Term Care Insurance
Long Term Care Insurance
jlucas0214
 
Overview 1.2
Overview 1.2Overview 1.2
Overview 1.2
poetawan
 
Website intro 280910
Website intro 280910Website intro 280910
Website intro 280910
Khoo Christie
 
Southland Power Summary.Mar 09
Southland Power Summary.Mar 09Southland Power Summary.Mar 09
Southland Power Summary.Mar 09
guestd2111a
 
Ad

Similar to 2CPP03 - Object Orientation Fundamentals (20)

2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
Michael Heron
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
Introduction to c ++ part -1
Introduction to c ++   part -1Introduction to c ++   part -1
Introduction to c ++ part -1
baabtra.com - No. 1 supplier of quality freshers
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
Jacob Green
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
Michael Heron
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
2CPP02 - C++ Primer
2CPP02 - C++ Primer2CPP02 - C++ Primer
2CPP02 - C++ Primer
Michael Heron
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
Salesforce Developers
 
object oriented programming_Classes_Objects.pptx
object oriented programming_Classes_Objects.pptxobject oriented programming_Classes_Objects.pptx
object oriented programming_Classes_Objects.pptx
WasiqAslam1
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
C++ Programming with examples for B.Tech
C++ Programming with examples for B.TechC++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
ashutoshgupta1102
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
JavedKhan524377
 
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.pptChapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
techtrainer11
 
Aspdot
AspdotAspdot
Aspdot
Nishad Nizarudeen
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
Fisnik Doko
 
OOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptxOOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptx
uzairrrfr
 
2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers2CPP06 - Arrays and Pointers
2CPP06 - Arrays and Pointers
Michael Heron
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
Vu Tran Lam
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
Connex
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
C++ Introduction brown bag
C++ Introduction brown bagC++ Introduction brown bag
C++ Introduction brown bag
Jacob Green
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
Michael Heron
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
Dhaval Kaneria
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
Salesforce Developers
 
object oriented programming_Classes_Objects.pptx
object oriented programming_Classes_Objects.pptxobject oriented programming_Classes_Objects.pptx
object oriented programming_Classes_Objects.pptx
WasiqAslam1
 
2CPP13 - Operator Overloading
2CPP13 - Operator Overloading2CPP13 - Operator Overloading
2CPP13 - Operator Overloading
Michael Heron
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
Geophery sanga
 
C++ Programming with examples for B.Tech
C++ Programming with examples for B.TechC++ Programming with examples for B.Tech
C++ Programming with examples for B.Tech
ashutoshgupta1102
 
lecture_for programming and computing basics
lecture_for programming and computing basicslecture_for programming and computing basics
lecture_for programming and computing basics
JavedKhan524377
 
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.pptChapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
Chapter10_Data_Abstraction_and_Object_Orientation_4e.ppt
techtrainer11
 
C# 7 development
C# 7 developmentC# 7 development
C# 7 development
Fisnik Doko
 
OOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptxOOP02-27022023-090456am.pptx
OOP02-27022023-090456am.pptx
uzairrrfr
 
Ad

More from Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
Michael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
Michael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
Michael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
Michael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
Michael Heron
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
Michael Heron
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
Michael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
Michael Heron
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
Michael Heron
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
Michael Heron
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
Michael Heron
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
Michael Heron
 
Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
Michael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
Michael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
Michael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
Michael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
Michael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
Michael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
Michael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
Michael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
Michael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
Michael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
Michael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
Michael Heron
 

Recently uploaded (20)

The Elixir Developer - All Things Open
The Elixir Developer - All Things OpenThe Elixir Developer - All Things Open
The Elixir Developer - All Things Open
Carlo Gilmar Padilla Santana
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Adobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREEAdobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREE
zafranwaqar90
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 
Solar-wind hybrid engery a system sustainable power
Solar-wind  hybrid engery a system sustainable powerSolar-wind  hybrid engery a system sustainable power
Solar-wind hybrid engery a system sustainable power
bhoomigowda12345
 
Programs as Values - Write code and don't get lost
Programs as Values - Write code and don't get lostPrograms as Values - Write code and don't get lost
Programs as Values - Write code and don't get lost
Pierangelo Cecchetto
 
Why Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card ProvidersWhy Tapitag Ranks Among the Best Digital Business Card Providers
Why Tapitag Ranks Among the Best Digital Business Card Providers
Tapitag
 
Sequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptxSequence Diagrams With Pictures (1).pptx
Sequence Diagrams With Pictures (1).pptx
aashrithakondapalli8
 
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
Surviving a Downturn Making Smarter Portfolio Decisions with OnePlan - Webina...
OnePlan Solutions
 
Adobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 linkAdobe InDesign Crack FREE Download 2025 link
Adobe InDesign Crack FREE Download 2025 link
mahmadzubair09
 
[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts[gbgcpp] Let's get comfortable with concepts
[gbgcpp] Let's get comfortable with concepts
Dimitrios Platis
 
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb ClarkDeploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Deploying & Testing Agentforce - End-to-end with Copado - Ewenb Clark
Peter Caitens
 
AEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural MeetingAEM User Group DACH - 2025 Inaugural Meeting
AEM User Group DACH - 2025 Inaugural Meeting
jennaf3
 
Adobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREEAdobe Audition Crack FRESH Version 2025 FREE
Adobe Audition Crack FRESH Version 2025 FREE
zafranwaqar90
 
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by AjathMobile Application Developer Dubai | Custom App Solutions by Ajath
Mobile Application Developer Dubai | Custom App Solutions by Ajath
Ajath Infotech Technologies LLC
 
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business StageA Comprehensive Guide to CRM Software Benefits for Every Business Stage
A Comprehensive Guide to CRM Software Benefits for Every Business Stage
SynapseIndia
 
Exchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv SoftwareExchange Migration Tool- Shoviv Software
Exchange Migration Tool- Shoviv Software
Shoviv Software
 
How to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber PluginHow to Install and Activate ListGrabber Plugin
How to Install and Activate ListGrabber Plugin
eGrabber
 
Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025Wilcom Embroidery Studio Crack Free Latest 2025
Wilcom Embroidery Studio Crack Free Latest 2025
Web Designer
 
What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?What Do Candidates Really Think About AI-Powered Recruitment Tools?
What Do Candidates Really Think About AI-Powered Recruitment Tools?
HireME
 
Adobe Media Encoder Crack FREE Download 2025
Adobe Media Encoder  Crack FREE Download 2025Adobe Media Encoder  Crack FREE Download 2025
Adobe Media Encoder Crack FREE Download 2025
zafranwaqar90
 
How I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetryHow I solved production issues with OpenTelemetry
How I solved production issues with OpenTelemetry
Cees Bos
 
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptxThe-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
The-Future-is-Hybrid-Exploring-Azure’s-Role-in-Multi-Cloud-Strategies.pptx
james brownuae
 

2CPP03 - Object Orientation Fundamentals

  • 2. Introduction • In this lecture we are going to look at the structure and syntax of an object oriented program in C++. • In theory, it is the same as in Java. • We shall also cover briefly the things that you should remember from your previous exposure to the idea of OOP. • We’ll go into it all in much more detail as the course progresses.
  • 3. Reminder of Concepts • The basic building blocks of an Object Oriented program are classes and objects. • Classes define a blueprint, or structure • Objects define the state. • An object is an instantiation of a class. • The class tells the object what structure and data fields it should have. • The object contains the value of those data fields.
  • 4. Reminder of Concepts • An Object Oriented program combined objects together to achieve some total goal. • Each object represents one part of a subdivision of labour. • You can think of a class as a complex, user-defined data type. • You can create multiple objects from a single class. • Usually
  • 5. OOP Good Practise • Methods and Attributes in an Object Oriented program have degrees of visiboility. • Variables should almost always be private. • They are modified through public accessor methods that you define. • Methods should have the level of visibility that minimises the impact of change associated with them. • More on this later, it’s an important concept.
  • 6. Declaring A Class (Java) public class Car { private double price; private String colour; public void setPrice (double p) { price = p; } public double getPrice() { return price; } public void setColour (String c) { colour = c; } public String getColour() { return colour; } }
  • 7. Declaring a Class (C++) #include <iostream> using namespace std; class Car { private: float price; string colour; public: void set_price (float p); float query_price(); void set_colour (string c); string query_colour(); };
  • 8. Differences • Visibility declared in groups in C++ • Defined at the method/variable level in Java • Class declaration ends with a semi-colon in C++ • No such syntactic requirement in Java • Difference in naming conventions. • Not enforced by the compiler, but an adopted stylistic convention. • Code definitions separated from code declarations.
  • 9. A C++ Class • A C++ class usually exists in two files. • The header file defining the class declaration. • That’s what we just say. • The code file defining the method bodies. • In Visual Studio, the wizards will handle this separation for you. • Other IDEs will most likely not. • C++ uses the scope resolution operator (::) to define code as belonging to a member function of a class.
  • 10. Car Code File #include "car.h" void Car::set_price (float p) { price = p; } float Car::query_price() { return price; } void Car::set_colour (string c) { colour = c; } string Car::query_colour() { return colour; }
  • 11. Instantiating an Object in C++ • In Java, this was done using the new keyword. • In C++, we also use new, but we also must declare a pointer to the object we are about to create: • When we have a pointer to an object, we use a -> operator to access defined methods. • Variables too, but don’t do that. • This is analagous to the dot operator in Java. • Be careful – C++ also has a dot operator. • It doesn’t work work with pointers.
  • 12. Using A Class #include <iostream> #include "car.h” int main() { Car *my_car; my_car = new Car(); my_car->set_price (100.0); cout << my_car->query_price() << endl; return 1; }
  • 13. Instantiating An Object (2) • You can also instantiate objects without using pointer notation. • You access the methods and variables using the dot notation, just as in Java. • You don’t use the new keyword, you just declare the variable. • The object then exists on the stack. • When working with this object though, you will be working with the value of it. • This is somewhat limiting, as you cannot make changes to the state of object outside of its context.
  • 14. Instantiating An Object (2) #include <iostream> #include "car.h" void set_value (Car, float); int main() { Car my_other_car; my_other_car.set_colour ("blue"); set_value (my_other_car, 100.0); cout << my_other_car.query_colour() << endl; cout << my_other_car.query_price() << endl; return 1; } void set_value (Car myCar, float val) { myCar.set_price (val); }
  • 15. Classes and Pointers • The most flexible way to work with objects in C++ (and the way that will most closely map onto your experience with objects in Java) is through pointers. • The output for the program shown the slide before would be: blue 0.0
  • 16. Classes and Pointers #include <iostream> #include "car.h" void set_value (Car*, float); int main() { Car* my_car; my_car = new Car(); my_car->set_colour ("blue"); set_value (my_car, 100.0); cout << my_car->query_colour() << endl; cout << my_car->query_price() << endl; return 1; } void set_value (Car *myCar, float val) { myCar->set_price (val); }
  • 17. Java Memory Management • One of the features of java is the garbage collector. • Essentially it does all the cleaning up of objects that are no longer in use in a program. • It means you don’t need to explicitly destroy objects once you are done with them. • The garbage collector will get to them eventually. • Usually • C++ requires you to destroy pointers to objects when you are done with them. • Or they stay in memory forever. • Until the program finishes execution.
  • 18. C++ Memory Management • Whenever you are completely finished with an object in C++, you must delete it. • This frees up the memory that it had previously been assigned. • From that point on, the memory is null memory. • You’ll get an error message if you try to access it. • The delete keyword is used to free up memory: delete my_car;
  • 19. Pointers and Objects • Those of you who are especially observant may have noticed some differences in the way we work with pointers when dealing with primitive data types and with objects. • There’s no inconsistency really, it’s just a consequence of how they are defined. • Remember what our two pointer operators actually do. • * means ‘the value of’ • & means ‘the address of’
  • 20. Pointers in Objects • When we declare a class as a pointer, we are saying ‘we are working with the value of this memory location’ • When we pass it into the function, we are already working with a pointer. • Thus we do not need to use the & operator when passing the parameter. • Within the function, we can use the variable name without the * operator. • The -> operator works directly on sections of memory.
  • 21. Pointers in C++ • This can be tremendously confusing. • It does become clear as time goes by, but expect this to be one of the hurdles you must overcome to be a confident C++ programmer. • As a result of this, the tutorial exercises tomorrow are on pointers and pointer manipulation. • Wear your thinkin’ caps!
  • 22. Summary • In the abstract, C++ classes work just like Java classes. • There are syntactical and structural differences. • The issue is further complicated by C++’s reliance on pointers. • Very powerful, but very confusing to begin with. • Remember that Java does a fair degree of ‘administration’ for you. • C++ is not quite so thoughtful!
  翻译: