Classes and Objects
Classes and Objects
OOP
OOP Approach
• Object?
Object –identical unit
Attributes & Behaviors in OOP
• Take a minute and think about what do you understand when you hear the
word “chair”
• The idea of the chair allows you to recognize a chair. However, the definition does not exist
• However, if you look around the room, there are several chairs are in the room, and each
chair is an object. Each of these objects is an example of that thing called the Chair (class)
• Produce a detailed design for each new data type (Objects) including
the operations that can be performed on each object
• Express logic of your program in terms of objects and kinds of
operations they allow
Class
• To declare a class
• – The class keyword is used
• The declaration is enclosed in braces {};
• The class definition ends with a semicolon
• Every piece of code inside the braces is called ‘class body’
• The class body thus contains class members
C++ Class – Declaration
•Class classname
•{
•………….
•…………
•…………….
•};
C++ Class – Declaration
• Every piece of code inside the braces is called ‘class body’
Classes
(Templates
)
Attributes
Behaviors
C++ Class – Declaration
Classes
(Templates
)
Data
Functions
C++ Class – Declaration
Classes
(Templates
)
Data Members
Class
Members
Member Functions
Class
•Class Student
•{
– Attributes
• Roll number
• Name
• Marks
– Behavior:
• Curricular performance
• Extra curricular performance
•} ;
Class
C++ Class – Data Members
• class Base {
• public:
• // public members go here
• protected:
•// protected members go here
• private:
• // private members go here
•};
public:
Line Comment
// sample C++ program
Pre processor
#include <iostream>
directive
Which
using namespace std; namespace to
int main() use
Main function
{ beginning
beginning of
cout << "Hello, there!"; block for main
return 0;
}
Ending of block
for main
Comments
• Document programs
• Improve program readability
• Ignored by compiler
• Single-line comment: Begin with //
• Multiple-line comment: Begin with /* and end with */
• Lines beginning with a hash sign (#) are directives read and
interpreted by what is known as the preprocessor.
• They are special lines interpreted before the compilation of the
program itself begins.
# include
• Both the user and the system header files are included using the
preprocessing directive #include. It has the following two forms −
• #include <file>
• #include "file"
# include <file>, "file"
1. Key Words
2. Programmer-Defined Identifiers
3. Operators
4. Punctuation/Separators
5. White Space
Keywords
• VALID
age_of_dog _taxRateY2K PrintHeading
ageOfHorse
• NOT VALID
age# 2000TaxRate Age-Of-Dog main
Identifiers..
Stream Operators – Input and Output
int num;
cout<< “Enter number\n \n”;
cin >> num;
52 Punctuation
• Characters that mark the end of the program.
• Characters that separate items in the list.
• Characters use for scope resolution
• types of punctuation marks
,
;
:
Variables
int main()
{
cars a;
int x,y;
x = a.c_model();
cout << x; Output?
cout << a.c_modelNo;
}
Object initialization
class cars
{
int c_modelNo();
};
Object’s name.class member
cars a;
a.c_modelNo();
Class Scope
•
Scope resolution operator is denoted with “::”
• Also called binary Scope resolution operator.
• This is used to refer any Data member of the class or Member
Function of the class.
Scope resolution Operator
class cars
{
int car_modelNo();
}
int cars::car_modelNo()
{
---------
}
Question!
• To make any function as inline, start its definitions with the keyword “inline”.
class cars
{
int car_modelNo();
}
#include <iostream>
using namespace std;
class queue
{
int qval;
public:
void setval(int v)
{
qval = v;
}
…
Example….
• int getval()
• {
• return qval;
• }
• };
Example….
• int main()
• {
• queue obj1, obj2;
• obj1.setval(5);
• obj2.setval(6);
• obj1.setval(4);
4 6 4