SlideShare a Scribd company logo
1 of 11
CSE240 – Introduction to
Programming Languages
Lecture 12:
Programming with C++ | Getting Started
Javier Gonzalez-Sanchez
javiergs@asu.edu
javiergs.engineering.asu.edu
Office Hours: By appointment
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2
Outline
1. Getting Started
• namespace,
• classes,
• scope(public, protected and private),
• input and output,
• scope resolution operator
2. Memory management
• static,
• constructor and destructor,
• delete
3. Inheritance and polymorphism
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3
Anatomy of a Program
class FooBar {
int variable;
void method() {
}
};
int x;
int main(){
FooBar anObject;
}
§ Data and the operations,
that manipulate data, are
encapsulated in classes.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4
Anatomy of a Program
class FooBar {
private:
char a;
int variable;
protected:
int anotherVariable;
public:
void method1() { }
void method2() { }
};
int x;
int main(){
FooBar anObject;
}
§ Data and the operations,
that manipulate data, are
encapsulated in classes.
§ You can defined public,
private, and protected
components. And,
encapsulated data can
only be accessed (from
outside the class)
through their interface
(operations defined as
public)
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5
Anatomy of a Program
class Shape {
};
class Rectangle: public Shape {
private:
FooBar a;
};
class FooBar {
};
int main(){
FooBar anObject;
}
§ Data and the operations,
that manipulate data, are
encapsulated in classes.
§ You can defined public,
private, and protected
components. And,
encapsulated data can
only be accessed (from
outside the class)
through their interface
(operations defined as
public)
§ Classes are organized in
hierarchies: use and
inherit.
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6
Inside each C++ class, it is similar to C program
C++
Java
C
Level of Abstraction
In Java,
attributes and methods are always in
one file.
Java have all related information in
one place.
In C++,
implementations of member functions
can be in the same file than the
class definition (for short
functions) or outside of the class
definition.
C++ argument: structurally clearer
to separate implementation from
definition
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7
Example in One File: queue.cpp
1. #include <iostream>
2. using namespace std;
3. class Queue {
4. private:
5. int queue_size;
6. protected:
7. int *buffer;
8. int front;
9. int rear;
10. public:
11. Queue(int v) {
12. cout<<"constructorn";
13. }
14. void enqueue(int v) {
15. cout<<"enqueuen";
16. }
17. int dequeue(void){
18. cout<<"dequeuen"; return 5;
19. }
20. };
21. int main(){
22. Queue q1(5);
23. Queue *q2 = new Queue(5);
24.
25. // Access Object
26. q1.enqueue(2);
27. q1.enqueue(8);
28.
29. // Access Object Pointer
30. q2->enqueue(25);
31. int x = q2->dequeue();
32. return 0;
33. }
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8
Example in Two Files: time.h
1. class Time {
2. public:
3. Time(); // constructor
4. void setTime( int, int ); // set hour, minute
5. void printMilitary(); // print military time format
6. void printStandard(); // print standard time format
7.
8. private:
9. int hour; // 0 - 23
10. int minute; // 0 - 59
11. };
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9
Example in Two Files: time.cpp
1. #include <iostream>
2. #include "time.h”
3. using namespace std;
4. Time::Time() { // constructor
5. hour = minute = 0;
6. }
7. void Time::setTime( int h, int m) { // Set a new mil Time
8. hour = ( h >= 0 && h < 24 ) ? h : 0;
9. minute = ( m >= 0 && m < 60 ) ? m : 0;
10. }
11. void Time::printMilitary() { // Print time in military format
12. cout << ( hour < 10 ? "0" : "" ) << hour << ":"
13. << ( minute < 10 ? "0" : "" ) << minute; // add "0"
14. }
Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10
Example in Two Files: time.cpp
15. void Time::printStandard() { // Print in standard format
16. cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
17. << ":" << ( minute < 10 ? "0" : "" ) << minute
18. << ( hour < 12 ? " AM" : " PM" ) << endl; //endl is equal to “n”
19. }
20.
21. int main() {
22. Time t; // new is not mandatory - instantiate object t of class Time
23. cout << "The initial military time is ";
24. t.printMilitary();
25. cout << "nThe initial standard time is ";
26. t.printStandard();
27. t.setTime(15, 27);
28. cout << "nnMilitary time after setTime is ";
29. t.printMilitary();
30. cout << "nStandard time after setTime is ";
31. t.printStandard();
32. return 0;
33. }
CSE240 – Introduction to Programming Languages
Javier Gonzalez-Sanchez
javiergs@asu.edu
Fall 2017
Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.

More Related Content

What's hot

Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"OdessaJS Conf
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleAxway Appcelerator
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"OdessaJS Conf
 
Travel management
Travel managementTravel management
Travel management1Parimal2
 
Kapacitor Alert Topic handlers
Kapacitor Alert Topic handlersKapacitor Alert Topic handlers
Kapacitor Alert Topic handlersInfluxData
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklistilsamaryum
 
All you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsAll you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsBrainhub
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CSteffen Wenz
 
Do while loop
Do while loopDo while loop
Do while loopBU
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolvedtrxcllnt
 

What's hot (20)

Roslyn: el futuro de C#
Roslyn: el futuro de C#Roslyn: el futuro de C#
Roslyn: el futuro de C#
 
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
Timur Shemsedinov "Пишу на колбеках, а что... (Асинхронное программирование)"
 
Richard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL ModuleRichard Salter: Using the Titanium OpenGL Module
Richard Salter: Using the Titanium OpenGL Module
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
Yurii Shevtsov "V8 + libuv = Node.js. Under the hood"
 
Travel management
Travel managementTravel management
Travel management
 
C++ file
C++ fileC++ file
C++ file
 
Kapacitor Alert Topic handlers
Kapacitor Alert Topic handlersKapacitor Alert Topic handlers
Kapacitor Alert Topic handlers
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklist
 
All you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, GeneratorsAll you need to know about Callbacks, Promises, Generators
All you need to know about Callbacks, Promises, Generators
 
Ns tutorial
Ns tutorialNs tutorial
Ns tutorial
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
1
11
1
 
Do while loop
Do while loopDo while loop
Do while loop
 
C++ assignment
C++ assignmentC++ assignment
C++ assignment
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Ruby haskell extension
Ruby haskell extensionRuby haskell extension
Ruby haskell extension
 
RxJS Evolved
RxJS EvolvedRxJS Evolved
RxJS Evolved
 
Libtcc and gwan
Libtcc and gwanLibtcc and gwan
Libtcc and gwan
 

Similar to 201801 CSE240 Lecture 12

What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11Henry Schreiner
 
Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#CodeOps Technologies LLP
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstractionHoang Nguyen
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshuSidd Singh
 
Computer Science Sample Paper 2
Computer Science Sample Paper 2Computer Science Sample Paper 2
Computer Science Sample Paper 2kvs
 
Computerscience 12th
Computerscience 12thComputerscience 12th
Computerscience 12thJUSTJOINUS
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksMongoDB
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiMuhammed Thanveer M
 
Operating System Lab Manual
Operating System Lab ManualOperating System Lab Manual
Operating System Lab ManualBilal Mirza
 
CS Sample Paper 1
CS Sample Paper 1CS Sample Paper 1
CS Sample Paper 1kvs
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidEmanuele Di Saverio
 

Similar to 201801 CSE240 Lecture 12 (20)

201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
What's new in Python 3.11
What's new in Python 3.11What's new in Python 3.11
What's new in Python 3.11
 
Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#Compiler Case Study - Design Patterns in C#
Compiler Case Study - Design Patterns in C#
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
 
C chap16
C chap16C chap16
C chap16
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Computer Science Sample Paper 2
Computer Science Sample Paper 2Computer Science Sample Paper 2
Computer Science Sample Paper 2
 
Computerscience 12th
Computerscience 12thComputerscience 12th
Computerscience 12th
 
Fun Teaching MongoDB New Tricks
Fun Teaching MongoDB New TricksFun Teaching MongoDB New Tricks
Fun Teaching MongoDB New Tricks
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
Python basics
Python basicsPython basics
Python basics
 
Operating System Lab Manual
Operating System Lab ManualOperating System Lab Manual
Operating System Lab Manual
 
CS Sample Paper 1
CS Sample Paper 1CS Sample Paper 1
CS Sample Paper 1
 
Novidades do c# 7 e 8
Novidades do c# 7 e 8Novidades do c# 7 e 8
Novidades do c# 7 e 8
 
Gradle plugins 101
Gradle plugins 101Gradle plugins 101
Gradle plugins 101
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
Programming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for AndroidProgramming Sideways: Asynchronous Techniques for Android
Programming Sideways: Asynchronous Techniques for Android
 
Final DAA_prints.pdf
Final DAA_prints.pdfFinal DAA_prints.pdf
Final DAA_prints.pdf
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201801 SER332 Lecture 04
201801 SER332 Lecture 04201801 SER332 Lecture 04
201801 SER332 Lecture 04
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 
201801 CSE240 Lecture 08
201801 CSE240 Lecture 08201801 CSE240 Lecture 08
201801 CSE240 Lecture 08
 
201801 CSE240 Lecture 07
201801 CSE240 Lecture 07201801 CSE240 Lecture 07
201801 CSE240 Lecture 07
 
201801 CSE240 Lecture 06
201801 CSE240 Lecture 06201801 CSE240 Lecture 06
201801 CSE240 Lecture 06
 

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

201801 CSE240 Lecture 12

  • 1. CSE240 – Introduction to Programming Languages Lecture 12: Programming with C++ | Getting Started Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu Office Hours: By appointment
  • 2. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 2 Outline 1. Getting Started • namespace, • classes, • scope(public, protected and private), • input and output, • scope resolution operator 2. Memory management • static, • constructor and destructor, • delete 3. Inheritance and polymorphism
  • 3. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 3 Anatomy of a Program class FooBar { int variable; void method() { } }; int x; int main(){ FooBar anObject; } § Data and the operations, that manipulate data, are encapsulated in classes.
  • 4. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 4 Anatomy of a Program class FooBar { private: char a; int variable; protected: int anotherVariable; public: void method1() { } void method2() { } }; int x; int main(){ FooBar anObject; } § Data and the operations, that manipulate data, are encapsulated in classes. § You can defined public, private, and protected components. And, encapsulated data can only be accessed (from outside the class) through their interface (operations defined as public)
  • 5. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 5 Anatomy of a Program class Shape { }; class Rectangle: public Shape { private: FooBar a; }; class FooBar { }; int main(){ FooBar anObject; } § Data and the operations, that manipulate data, are encapsulated in classes. § You can defined public, private, and protected components. And, encapsulated data can only be accessed (from outside the class) through their interface (operations defined as public) § Classes are organized in hierarchies: use and inherit.
  • 6. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 6 Inside each C++ class, it is similar to C program C++ Java C Level of Abstraction In Java, attributes and methods are always in one file. Java have all related information in one place. In C++, implementations of member functions can be in the same file than the class definition (for short functions) or outside of the class definition. C++ argument: structurally clearer to separate implementation from definition
  • 7. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 7 Example in One File: queue.cpp 1. #include <iostream> 2. using namespace std; 3. class Queue { 4. private: 5. int queue_size; 6. protected: 7. int *buffer; 8. int front; 9. int rear; 10. public: 11. Queue(int v) { 12. cout<<"constructorn"; 13. } 14. void enqueue(int v) { 15. cout<<"enqueuen"; 16. } 17. int dequeue(void){ 18. cout<<"dequeuen"; return 5; 19. } 20. }; 21. int main(){ 22. Queue q1(5); 23. Queue *q2 = new Queue(5); 24. 25. // Access Object 26. q1.enqueue(2); 27. q1.enqueue(8); 28. 29. // Access Object Pointer 30. q2->enqueue(25); 31. int x = q2->dequeue(); 32. return 0; 33. }
  • 8. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 8 Example in Two Files: time.h 1. class Time { 2. public: 3. Time(); // constructor 4. void setTime( int, int ); // set hour, minute 5. void printMilitary(); // print military time format 6. void printStandard(); // print standard time format 7. 8. private: 9. int hour; // 0 - 23 10. int minute; // 0 - 59 11. };
  • 9. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 9 Example in Two Files: time.cpp 1. #include <iostream> 2. #include "time.h” 3. using namespace std; 4. Time::Time() { // constructor 5. hour = minute = 0; 6. } 7. void Time::setTime( int h, int m) { // Set a new mil Time 8. hour = ( h >= 0 && h < 24 ) ? h : 0; 9. minute = ( m >= 0 && m < 60 ) ? m : 0; 10. } 11. void Time::printMilitary() { // Print time in military format 12. cout << ( hour < 10 ? "0" : "" ) << hour << ":" 13. << ( minute < 10 ? "0" : "" ) << minute; // add "0" 14. }
  • 10. Javier Gonzalez-Sanchez | CSE 240 | Fall 2017 | 10 Example in Two Files: time.cpp 15. void Time::printStandard() { // Print in standard format 16. cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) 17. << ":" << ( minute < 10 ? "0" : "" ) << minute 18. << ( hour < 12 ? " AM" : " PM" ) << endl; //endl is equal to “n” 19. } 20. 21. int main() { 22. Time t; // new is not mandatory - instantiate object t of class Time 23. cout << "The initial military time is "; 24. t.printMilitary(); 25. cout << "nThe initial standard time is "; 26. t.printStandard(); 27. t.setTime(15, 27); 28. cout << "nnMilitary time after setTime is "; 29. t.printMilitary(); 30. cout << "nStandard time after setTime is "; 31. t.printStandard(); 32. return 0; 33. }
  • 11. CSE240 – Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu Fall 2017 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.