SlideShare a Scribd company logo
1 of 32
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
CS 106X
Classes and Objects
guest presenter:
Marty Stepp (stepp AT cs DOT stanford DOT edu)
reading:
Programming Abstractions in C++, Chapter 6
2
Class examples
• A calendar program might want to store information
about dates, but C++ does not have a Date type.
• A student registration system needs to store info
about students, but C++ has no Student type.
• A bank app might want to store information about
users' accounts, but C++ has no BankAccount type.
• However, C++ does provide a feature for us to add
new data types to the language: classes.
– Writing a class defines a new data type.
3
Classes and objects (6.1)
• class: A program entity that represents
a template for a new type of objects.
– e.g. class Vector defines a new data type
named Vector and allows you to declare
objects of that type.
• object: Entity that combines state and behavior.
– object-oriented programming (OOP): Programs that perform their
behavior as interactions between objects.
– abstraction: Separation between concepts and details.
Objects provide abstraction in programming.
4
Client, class, object
Class
- what goes into each object
- how to construct new objects
Client program
int main() {
...
- interacts with class and objects
object object
object
constructs
Object
- member functions (public behavior)
memberFunction1()
memberFunction2()
- member variables (private data)
ivar1 [___] (encapsulated)
ivar2 [___]
asks class to construct a new object
send/receive messages with object
by calling member functions
(never directly access private data)
5
Elements of a class
• member variables: State inside each object.
– Also called "instance variables" or "fields"
– Declared as private
– Each object created has a copy of each field.
• member functions: Behavior that executes inside each object.
– Also called "methods"
– Each object created has a copy of each method.
– The method can interact with the data inside that object.
• constructor: Initializes new objects as they are created.
– Sets the initial state of each new object.
– Often accepts parameters for the initial state of the fields.
6
Interface vs. code
• In C++, when writing classes you must understand separation of:
– interface: Declarations of functions, classes, members, etc.
– implementation: Definitions of how the above are implemented.
• C++ implements this separation using two kinds of code files:
– .h: A "header" file containing only interface (declarations).
– .cpp: A "source" file containing definitions.
• When you define a new class Foo, you write Foo.h and Foo.cpp.
• The content of .h files is "#included" inside .cpp files.
– Makes them aware of declarations of code implemented elsewhere.
– At compilation, all definitions are linked together into an executable.
7
Structure of a .h file
// classname.h
#ifndef _classname_h
#define _classname_h
class declaration;
#endif
This is protection in case
multiple .cpp files include this .h,
so that its contents won't
get declared twice
8
A class declaration
class ClassName { // in ClassName.h
public:
ClassName(parameters); // constructor
returnType name(parameters); // member functions
returnType name(parameters); // (behavior inside
returnType name(parameters); // each object)
private:
type name; // member variables
type name; // (data inside each object)
};
IMPORTANT: must put a semicolon at end of class declaration (argh)
9
Class example (v1)
// Initial version of BankAccount.h.
// Uses public member variables and no functions.
// Not good style, but we will improve it.
#ifndef _bankaccount_h
#define _bankaccount_h
class BankAccount {
public:
string name; // each BankAccount object
double balance; // has a name and balance
};
#endif
10
Using objects
// v1 with public fields (bad)
BankAccount ba1;
ba1.name = "Marty";
ba1.balance = 1.25;
BankAccount ba2;
ba2.name = "Mehran";
ba2.balance = 9999.00;
• Think of an object as a way of grouping multiple variables.
– Each object contains a name and balance field inside it.
– We can get/set them individually.
– Code that uses your objects is called client code.
name = "Marty"
balance = 1.25
name = "Mehran"
balance = 9999.00
ba1
ba2
11
Member func. bodies
• In ClassName.cpp, we write bodies (definitions) for the member
functions that were declared in the .h file:
// ClassName.cpp
#include "ClassName.h"
// member function
returnType ClassName::methodName(parameters) {
statements;
}
– Member functions/constructors can refer to the object's fields.
• Exercise: Write a withdraw member function to deduct money
from a bank account's balance.
12
The implicit parameter
• implicit parameter:
The object on which a member function is called.
– During the call marty.withdraw(...),
the object named marty is the implicit parameter.
– During the call mehran.withdraw(...),
the object named mehran is the implicit parameter.
– The member function can refer to that object's member variables.
• We say that it executes in the context of a particular object.
• The function can refer to the data of the object it was called on.
• It behaves as if each object has its own copy of the member functions.
13
Member func diagram
// BankAccount.cpp
void BankAccount::withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
// client program
BankAccount marty;
BankAccount mehran;
...
marty.withdraw(5.00);
mehran.withdraw(99.00);
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
name "marty" balance 1.25
void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
}
}
name "mehran" balance 9999
14
Initializing objects
• It's bad to take 3 lines to create a BankAccount and initialize it:
BankAccount ba;
ba.name = "Marty";
ba.balance = 1.25; // tedious
• We'd rather specify the fields' initial values at the start:
BankAccount ba("Marty", 1.25); // better
– We are able to this with most types of objects in C++ and Java.
– You can achieve this functionality using a constructor.
15
Constructors
ClassName::ClassName(parameters) {
statements to initialize the object;
}
• constructor: Initializes state of new objects as they are created.
– runs when the client declares a new object
– no return type is specified;
it implicitly "returns" the new object being created
– If a class has no constructor, C++ gives it a default constructor with no
parameters that does nothing.
16
Constructor diagram
// BankAccount.cpp
BankAccount::BankAccount(string n, double b) {
name = n;
balance = b;
}
// client program
BankAccount b1(
"Marty", 1.25);
BankAccount b2(
"Mehran", 9999);
BankAccount(string n, double b) {
name = n;
balance = b;
}
name balance
BankAccount(string n, double b) {
name = n;
balance = b;
}
name balance
17
The keyword this
• As in Java, C++ has a this keyword to refer to the current object.
– Syntax: this->member
– Common usage: In constructor, so parameter names can match the
names of the object's member variables:
BankAccount::BankAccount(string name,
double balance) {
this->name = name;
this->balance = balance;
}
this uses -> not . because it is a "pointer"; we'll discuss that later
18
Preconditions
• precondition: Something your code assumes is true
at the start of its execution.
– Often documented as a comment on the function's header:
// Initializes a BankAccount with the given state.
// Precondition: balance is non-negative
BankAccount::BankAccount(string name, double balance) {
this->name = name;
this->balance = balance;
}
– Stating a precondition doesn't really "solve" the problem, but it at least
documents our decision and warns the client what not to do.
– What if we want to actually enforce the precondition?
19
Throwing exceptions
throw expression;
• Generates an exception that will crash the program,
unless it has code to handle ("catch") the exception.
// Initializes a BankAccount with the given state.
// Precondition: balance is non-negative
BankAccount::BankAccount(string name, double balance) {
if (balance < 0) {
throw "Illegal negative balance";
}
this->name = name;
this->balance = balance;
}
• Why would anyone ever want a program to crash?
20
Private data
private:
type name;
• encapsulation: Hiding implementation details of an
object from its clients.
– Encapsulation provides abstraction.
• separates external view (behavior) from internal view (state)
– Encapsulation protects the integrity of an object's data.
• A class's data members should be declared private.
– No code outside the class can access or change it.
21
Accessor functions
• We can provide methods to get and/or set a data field's value:
// "read-only" access to the balance ("accessor")
double BankAccount::getBalance() {
return balance;
}
// Allows clients to change the field ("mutator")
void BankAccount::setName(string newName) {
name = newName;
}
– Client code will look like this:
cout << ba.getName() << ":$" << ba.getBalance() << endl;
ba.setName("Cynthia");
22
Encapsulation benefits
• Provides abstraction between an object and its clients.
• Protects an object from unwanted access by clients.
• Allows you to change the class implementation.
– Point could be rewritten to use polar coordinates
(radius r, angle θ), but with the same methods.
• Allows you to constrain objects' state (invariants).
– Example: Don't allow a BankAccount with a negative balance.
23
Operator overloading (6.2)
• C++ allows you to overload, or redefine, the behavior of many
common operators in the language:
– unary: + - ++ -- * & ! ~ new delete
– binary: + - * / % += -= *= /= %= & | && || ^
== != < > <= >= = [] -> () ,
• Overuse of operator overloading can lead to confusing code.
– Rule of Thumb: Don't abuse this feature. Don't define an overloaded
operator unless its meaning and behavior are completely obvious.
24
Op overload syntax
• Declare your operator in a .h file, implement it in a .cpp file.
returnType operator op(parameters); // .h
returnType operator op(parameters) { // .cpp
statements;
};
– where op is some operator like +, ==, <<, etc.
– the parameters are the operands next to the operator;
for example, a + b becomes operator +(Foo a, Foo b)
Overloaded operators can also be declared inside a class (not shown here)
25
Op overload example
// BankAccount.h
class BankAccount {
...
};
bool operator ==(BankAccount& ba1, BankAccount& ba2);
bool operator !=(BankAccount& ba1, BankAccount& ba2);
// BankAccount.cpp
bool operator ==(BankAccount& ba1, BankAccount& ba2) {
return ba1.getName() == ba2.getName()
&& ba1.getBalance() == ba2.getBalance();
}
bool operator !=(BankAccount& ba1, BankAccount& ba2) {
return !(ba1 == ba2); // calls operator ==
}
26
Make objects printable
• To make it easy to print your object to cout, overload the <<
operator between an ostream and your type:
ostream& operator <<(ostream& out, Type& name) {
statements;
return out;
}
– The operator returns a reference to the stream so it can be chained.
•cout << a << b << c is really ((cout << a) << b) << c
• Technically cout is being returned by each << operation.
27
<< overload example
// BankAccount.h
class BankAccount {
...
};
ostream& operator <<(ostream& out, BankAccount& ba);
// BankAccount.cpp
ostream& operator <<(ostream& out, BankAccount& ba) {
out << ba.getName() << ": $"
<< setprecision(2) << ba.getBalance();
return out;
}
28
The keyword const
• C++ const keyword indicates that a value cannot change.
const int x = 4; // x will always be 4
• a const reference parameter can't be modified by the function:
void foo(const BankAccount& ba) { // won't change ba
• Any attempts to modify d inside foo's code won't compile.
• a const member function can't change the object's state:
class BankAccount { ...
double getBalance() const; // won't change account
• On a const reference, you can only call const member functions.
This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved.
Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others.
Overflow (extra) slides
30
Class constants
• To make a class constant, declare a static variable in the .h file.
– Assign its value in the .cpp, outside of any method.
– Don't write static or const when assigning the value in the .cpp.
// BankAccount.h
class BankAccount {
static const double INTEREST_RATE;
};
// BankAccount.cpp
double BankAccount::INTEREST_RATE = 0.0325; // 3.25%
31
Structs
• C++ also has an entity called a structure (struct).
– Very similar to a class; a collection of data and (maybe) behavior.
– But has (by default) public fields and no methods.
struct Point {
int x;
int y;
};
...
Point p;
p.x = 15;
...
– A holdover from C, which did not have classes or objects.
– Not used as often as classes, but you may see them from time to time.
32
C++ preprocessor
• preprocessor : Part of the C++ compilation process; recognizes
special # statements, modifies source code before it is compiled
function description
#include <filename> insert a library file's contents into this file
#include "filename" insert a user file's contents into this file
#define name [value] create a preprocessor symbol ("variable")
#if test if statement
#else else statement
#elif test else if statement
#endif terminates an if or if/else statement
#ifdef name if statement; true if name is defined
#ifndef name if statement; true if name is not defined
#undef name deletes the given symbol name

More Related Content

Similar to 11-Classes.ppt

JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & AnswersRatnala Charan kumar
 
C questions
C questionsC questions
C questionsparm112
 
computer notes - Reference variables ii
computer notes - Reference variables iicomputer notes - Reference variables ii
computer notes - Reference variables iiecomputernotes
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNico Ludwig
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plusSayed Ahmed
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Sayed Ahmed
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its TypesMuhammad Hammad Waseem
 
Ee java lab assignment 2
Ee java lab assignment 2Ee java lab assignment 2
Ee java lab assignment 2Kuntal Bhowmick
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And AnswerJagan Mohan Bishoyi
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answerlavparmar007
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptxsyedabbas594247
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxMegha V
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classsdjdskjd9097
 
Cis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variablesCis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variablessdjdskjd9097
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesccis224477
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classsdjdskjd9097
 

Similar to 11-Classes.ppt (20)

JavaScript(Es5) Interview Questions & Answers
JavaScript(Es5)  Interview Questions & AnswersJavaScript(Es5)  Interview Questions & Answers
JavaScript(Es5) Interview Questions & Answers
 
C questions
C questionsC questions
C questions
 
computer notes - Reference variables ii
computer notes - Reference variables iicomputer notes - Reference variables ii
computer notes - Reference variables ii
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
New c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_iNew c sharp3_features_(linq)_part_i
New c sharp3_features_(linq)_part_i
 
Introduction to c_plus_plus
Introduction to c_plus_plusIntroduction to c_plus_plus
Introduction to c_plus_plus
 
Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)Introduction to c_plus_plus (6)
Introduction to c_plus_plus (6)
 
Ch03
Ch03Ch03
Ch03
 
Ch03
Ch03Ch03
Ch03
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
Ee java lab assignment 2
Ee java lab assignment 2Ee java lab assignment 2
Ee java lab assignment 2
 
C++ Interview Question And Answer
C++ Interview Question And AnswerC++ Interview Question And Answer
C++ Interview Question And Answer
 
C++ questions And Answer
C++ questions And AnswerC++ questions And Answer
C++ questions And Answer
 
object oriented programming language.pptx
object oriented programming language.pptxobject oriented programming language.pptx
object oriented programming language.pptx
 
JavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptxJavaScript- Functions and arrays.pptx
JavaScript- Functions and arrays.pptx
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee class
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
Cis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variablesCis247 i lab 3 overloaded methods and static methods variables
Cis247 i lab 3 overloaded methods and static methods variables
 
Cis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variablesCis247 a ilab 3 overloaded methods and static methods variables
Cis247 a ilab 3 overloaded methods and static methods variables
 
Cis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee classCis247 i lab 2 of 7 employee class
Cis247 i lab 2 of 7 employee class
 

Recently uploaded

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

11-Classes.ppt

  • 1. This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others. CS 106X Classes and Objects guest presenter: Marty Stepp (stepp AT cs DOT stanford DOT edu) reading: Programming Abstractions in C++, Chapter 6
  • 2. 2 Class examples • A calendar program might want to store information about dates, but C++ does not have a Date type. • A student registration system needs to store info about students, but C++ has no Student type. • A bank app might want to store information about users' accounts, but C++ has no BankAccount type. • However, C++ does provide a feature for us to add new data types to the language: classes. – Writing a class defines a new data type.
  • 3. 3 Classes and objects (6.1) • class: A program entity that represents a template for a new type of objects. – e.g. class Vector defines a new data type named Vector and allows you to declare objects of that type. • object: Entity that combines state and behavior. – object-oriented programming (OOP): Programs that perform their behavior as interactions between objects. – abstraction: Separation between concepts and details. Objects provide abstraction in programming.
  • 4. 4 Client, class, object Class - what goes into each object - how to construct new objects Client program int main() { ... - interacts with class and objects object object object constructs Object - member functions (public behavior) memberFunction1() memberFunction2() - member variables (private data) ivar1 [___] (encapsulated) ivar2 [___] asks class to construct a new object send/receive messages with object by calling member functions (never directly access private data)
  • 5. 5 Elements of a class • member variables: State inside each object. – Also called "instance variables" or "fields" – Declared as private – Each object created has a copy of each field. • member functions: Behavior that executes inside each object. – Also called "methods" – Each object created has a copy of each method. – The method can interact with the data inside that object. • constructor: Initializes new objects as they are created. – Sets the initial state of each new object. – Often accepts parameters for the initial state of the fields.
  • 6. 6 Interface vs. code • In C++, when writing classes you must understand separation of: – interface: Declarations of functions, classes, members, etc. – implementation: Definitions of how the above are implemented. • C++ implements this separation using two kinds of code files: – .h: A "header" file containing only interface (declarations). – .cpp: A "source" file containing definitions. • When you define a new class Foo, you write Foo.h and Foo.cpp. • The content of .h files is "#included" inside .cpp files. – Makes them aware of declarations of code implemented elsewhere. – At compilation, all definitions are linked together into an executable.
  • 7. 7 Structure of a .h file // classname.h #ifndef _classname_h #define _classname_h class declaration; #endif This is protection in case multiple .cpp files include this .h, so that its contents won't get declared twice
  • 8. 8 A class declaration class ClassName { // in ClassName.h public: ClassName(parameters); // constructor returnType name(parameters); // member functions returnType name(parameters); // (behavior inside returnType name(parameters); // each object) private: type name; // member variables type name; // (data inside each object) }; IMPORTANT: must put a semicolon at end of class declaration (argh)
  • 9. 9 Class example (v1) // Initial version of BankAccount.h. // Uses public member variables and no functions. // Not good style, but we will improve it. #ifndef _bankaccount_h #define _bankaccount_h class BankAccount { public: string name; // each BankAccount object double balance; // has a name and balance }; #endif
  • 10. 10 Using objects // v1 with public fields (bad) BankAccount ba1; ba1.name = "Marty"; ba1.balance = 1.25; BankAccount ba2; ba2.name = "Mehran"; ba2.balance = 9999.00; • Think of an object as a way of grouping multiple variables. – Each object contains a name and balance field inside it. – We can get/set them individually. – Code that uses your objects is called client code. name = "Marty" balance = 1.25 name = "Mehran" balance = 9999.00 ba1 ba2
  • 11. 11 Member func. bodies • In ClassName.cpp, we write bodies (definitions) for the member functions that were declared in the .h file: // ClassName.cpp #include "ClassName.h" // member function returnType ClassName::methodName(parameters) { statements; } – Member functions/constructors can refer to the object's fields. • Exercise: Write a withdraw member function to deduct money from a bank account's balance.
  • 12. 12 The implicit parameter • implicit parameter: The object on which a member function is called. – During the call marty.withdraw(...), the object named marty is the implicit parameter. – During the call mehran.withdraw(...), the object named mehran is the implicit parameter. – The member function can refer to that object's member variables. • We say that it executes in the context of a particular object. • The function can refer to the data of the object it was called on. • It behaves as if each object has its own copy of the member functions.
  • 13. 13 Member func diagram // BankAccount.cpp void BankAccount::withdraw(double amount) { if (balance >= amount) { balance -= amount; } } // client program BankAccount marty; BankAccount mehran; ... marty.withdraw(5.00); mehran.withdraw(99.00); void withdraw(double amount) { if (balance >= amount) { balance -= amount; } } name "marty" balance 1.25 void withdraw(double amount) { if (balance >= amount) { balance -= amount; } } name "mehran" balance 9999
  • 14. 14 Initializing objects • It's bad to take 3 lines to create a BankAccount and initialize it: BankAccount ba; ba.name = "Marty"; ba.balance = 1.25; // tedious • We'd rather specify the fields' initial values at the start: BankAccount ba("Marty", 1.25); // better – We are able to this with most types of objects in C++ and Java. – You can achieve this functionality using a constructor.
  • 15. 15 Constructors ClassName::ClassName(parameters) { statements to initialize the object; } • constructor: Initializes state of new objects as they are created. – runs when the client declares a new object – no return type is specified; it implicitly "returns" the new object being created – If a class has no constructor, C++ gives it a default constructor with no parameters that does nothing.
  • 16. 16 Constructor diagram // BankAccount.cpp BankAccount::BankAccount(string n, double b) { name = n; balance = b; } // client program BankAccount b1( "Marty", 1.25); BankAccount b2( "Mehran", 9999); BankAccount(string n, double b) { name = n; balance = b; } name balance BankAccount(string n, double b) { name = n; balance = b; } name balance
  • 17. 17 The keyword this • As in Java, C++ has a this keyword to refer to the current object. – Syntax: this->member – Common usage: In constructor, so parameter names can match the names of the object's member variables: BankAccount::BankAccount(string name, double balance) { this->name = name; this->balance = balance; } this uses -> not . because it is a "pointer"; we'll discuss that later
  • 18. 18 Preconditions • precondition: Something your code assumes is true at the start of its execution. – Often documented as a comment on the function's header: // Initializes a BankAccount with the given state. // Precondition: balance is non-negative BankAccount::BankAccount(string name, double balance) { this->name = name; this->balance = balance; } – Stating a precondition doesn't really "solve" the problem, but it at least documents our decision and warns the client what not to do. – What if we want to actually enforce the precondition?
  • 19. 19 Throwing exceptions throw expression; • Generates an exception that will crash the program, unless it has code to handle ("catch") the exception. // Initializes a BankAccount with the given state. // Precondition: balance is non-negative BankAccount::BankAccount(string name, double balance) { if (balance < 0) { throw "Illegal negative balance"; } this->name = name; this->balance = balance; } • Why would anyone ever want a program to crash?
  • 20. 20 Private data private: type name; • encapsulation: Hiding implementation details of an object from its clients. – Encapsulation provides abstraction. • separates external view (behavior) from internal view (state) – Encapsulation protects the integrity of an object's data. • A class's data members should be declared private. – No code outside the class can access or change it.
  • 21. 21 Accessor functions • We can provide methods to get and/or set a data field's value: // "read-only" access to the balance ("accessor") double BankAccount::getBalance() { return balance; } // Allows clients to change the field ("mutator") void BankAccount::setName(string newName) { name = newName; } – Client code will look like this: cout << ba.getName() << ":$" << ba.getBalance() << endl; ba.setName("Cynthia");
  • 22. 22 Encapsulation benefits • Provides abstraction between an object and its clients. • Protects an object from unwanted access by clients. • Allows you to change the class implementation. – Point could be rewritten to use polar coordinates (radius r, angle θ), but with the same methods. • Allows you to constrain objects' state (invariants). – Example: Don't allow a BankAccount with a negative balance.
  • 23. 23 Operator overloading (6.2) • C++ allows you to overload, or redefine, the behavior of many common operators in the language: – unary: + - ++ -- * & ! ~ new delete – binary: + - * / % += -= *= /= %= & | && || ^ == != < > <= >= = [] -> () , • Overuse of operator overloading can lead to confusing code. – Rule of Thumb: Don't abuse this feature. Don't define an overloaded operator unless its meaning and behavior are completely obvious.
  • 24. 24 Op overload syntax • Declare your operator in a .h file, implement it in a .cpp file. returnType operator op(parameters); // .h returnType operator op(parameters) { // .cpp statements; }; – where op is some operator like +, ==, <<, etc. – the parameters are the operands next to the operator; for example, a + b becomes operator +(Foo a, Foo b) Overloaded operators can also be declared inside a class (not shown here)
  • 25. 25 Op overload example // BankAccount.h class BankAccount { ... }; bool operator ==(BankAccount& ba1, BankAccount& ba2); bool operator !=(BankAccount& ba1, BankAccount& ba2); // BankAccount.cpp bool operator ==(BankAccount& ba1, BankAccount& ba2) { return ba1.getName() == ba2.getName() && ba1.getBalance() == ba2.getBalance(); } bool operator !=(BankAccount& ba1, BankAccount& ba2) { return !(ba1 == ba2); // calls operator == }
  • 26. 26 Make objects printable • To make it easy to print your object to cout, overload the << operator between an ostream and your type: ostream& operator <<(ostream& out, Type& name) { statements; return out; } – The operator returns a reference to the stream so it can be chained. •cout << a << b << c is really ((cout << a) << b) << c • Technically cout is being returned by each << operation.
  • 27. 27 << overload example // BankAccount.h class BankAccount { ... }; ostream& operator <<(ostream& out, BankAccount& ba); // BankAccount.cpp ostream& operator <<(ostream& out, BankAccount& ba) { out << ba.getName() << ": $" << setprecision(2) << ba.getBalance(); return out; }
  • 28. 28 The keyword const • C++ const keyword indicates that a value cannot change. const int x = 4; // x will always be 4 • a const reference parameter can't be modified by the function: void foo(const BankAccount& ba) { // won't change ba • Any attempts to modify d inside foo's code won't compile. • a const member function can't change the object's state: class BankAccount { ... double getBalance() const; // won't change account • On a const reference, you can only call const member functions.
  • 29. This document is copyright (C) Stanford Computer Science and Marty Stepp, licensed under Creative Commons Attribution 2.5 License. All rights reserved. Based on slides created by Keith Schwarz, Julie Zelenski, Jerry Cain, Eric Roberts, Mehran Sahami, Stuart Reges, Cynthia Lee, and others. Overflow (extra) slides
  • 30. 30 Class constants • To make a class constant, declare a static variable in the .h file. – Assign its value in the .cpp, outside of any method. – Don't write static or const when assigning the value in the .cpp. // BankAccount.h class BankAccount { static const double INTEREST_RATE; }; // BankAccount.cpp double BankAccount::INTEREST_RATE = 0.0325; // 3.25%
  • 31. 31 Structs • C++ also has an entity called a structure (struct). – Very similar to a class; a collection of data and (maybe) behavior. – But has (by default) public fields and no methods. struct Point { int x; int y; }; ... Point p; p.x = 15; ... – A holdover from C, which did not have classes or objects. – Not used as often as classes, but you may see them from time to time.
  • 32. 32 C++ preprocessor • preprocessor : Part of the C++ compilation process; recognizes special # statements, modifies source code before it is compiled function description #include <filename> insert a library file's contents into this file #include "filename" insert a user file's contents into this file #define name [value] create a preprocessor symbol ("variable") #if test if statement #else else statement #elif test else if statement #endif terminates an if or if/else statement #ifdef name if statement; true if name is defined #ifndef name if statement; true if name is not defined #undef name deletes the given symbol name