SlideShare a Scribd company logo
1
• A pointer variable is a variable whose value is the
address of a location in memory
int x;
x = 5;
int* ptr1;
ptr1 = &x;
int* ptr2;
ptr2 = ptr1;
*ptr1 = 6;
cout << ptr1 << endl;
cout << *ptr2 << endl;
Review: Pointers & Dynamic Data
int* ptr3;
ptr3 = new int;
*ptr3 = 5;
delete ptr3;
ptr3 = NULL;
int *ptr4;
ptr4 = new int[5];
ptr4[0] = 100;
ptr4[4] = 123;
delete [] ptr4;
ptr4 = NULL;
2
void increment(int b1, int &b2, int *b3)
{
b1 += 2;
b2 += 2
*b3 += 2;
}
Review: Reference Types
• Reference Types
– Alias for another variable
– Must be initialized when declared
– Are primarily used as function parameters
int main (void){
int a1 = 5, a2 = 10;
int *a3 = new int;
*a3 = 15;
int &a4 = a3;
cout << a1 << a2 << a3 << endl;
increment(a1, a2, a3);
cout << a1 << a2 << a3 << endl;
delete a3; a3 = NULL;
return 0;
}
3
Object-Oriented Programming
Introduction to Classes
• Class Definition
• Class Examples
• Objects
• Constructors
• Destructors
4
Class
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and inheritance
• Type
– Concrete representation of a concept
• Eg. float with operations like -, *, + (math real numbers)
• Class
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
• Advantages
– Types matching program concepts
• Game Program (Explosion type)
– Concise program
– Code analysis easy
– Compiler can detect illegal uses of types
• Data Abstraction
– Separate the implementation details from its essential properties
5
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Classes & Objects
Rectangle r1;
Rectangle r2;
Rectangle r3;
……
int a;
Objects: Instance of a class
6
Define a Class Type
class class_name
{
permission_label:
member;
permission_label:
member;
...
};
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Body
Header
7
Class Definition
Data Members
• Can be of any type, built-in or user-defined
• non-static data member
– Each class object has its own copy
• static data member
– Acts as a global variable
– One copy per class type, e.g. counter
8
class Rectangle
{
private:
int width;
int length;
static int count;
public:
void set(int w, int
l);
int area();
}
Static Data Member
Rectangle r1;
Rectangle r2;
Rectangle r3;
width
length
width
length
width
length
r1
r3
r2
count
9
Class Definition
Member Functions
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
10
Define a Member Function
class Rectangle
{
private:
int width, length;
public:
void set (int w, int l);
int area() {return width*length; }
};
void Rectangle :: set (int w, int l)
{
width = w;
length = l;
}
inline
class name
member function name
scope operator
r1.set(5,8);
rp->set(8,10);
11
• const member function
– declaration
• return_type func_name (para_list) const;
– definition
• return_type func_name (para_list) const { … }
• return_type class_name :: func_name (para_list) const { … }
– Makes no modification about the data members (safe
function)
– It is illegal for a const member function to modify a
class data member
Class Definition
Member Functions
12
Const Member Function
class Time
{
private :
int hrs, mins, secs ;
public :
void Write ( )
const ;
} ;
void Time :: Write( ) const
{
cout <<hrs << “:” << mins << “:” << secs << endl;
}
function declaration
function definition
13
• Information hiding
– To prevent the internal representation from direct
access from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends
of this class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program
Class Definition - Access Control
14
class Time Specification
class Time
{
public :
void Set ( int hours , int minutes , int seconds ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initHrs, int initMins, int initSecs ) ; // constructor
Time ( ) ; // default constructor
private :
int hrs ;
int mins ;
int secs ;
} ;
14
15
Class Interface Diagram
Private data:
hrs
mins
secs
Set
Increment
Write
Time
Time
Time class
16
• The default access specifier is private
• The data members are usually private or protected
• A private member function is a helper, may only be
accessed by another member function of the same
class (exception friend function)
• The public member functions are part of the class
interface
• Each access control section is optional, repeatable,
and sections may occur in any order
Class Definition
Access Control
17
What is an object?
OBJECT
Operations
Data
set of methods
(member functions)
internal state
(values of private data members)
18
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
Rectangle r2;
r1.set(5, 8);
cout<<r1.area()<<endl;
r2.set(8,10);
cout<<r2.area()<<endl;
}
19
Another Example
#include <iostream.h>
class circle
{
private:
double radius;
public:
void store(double);
double area(void);
void display(void);
};
// member function definitions
void circle::store(double r)
{
radius = r;
}
double circle::area(void)
{
return 3.14*radius*radius;
}
void circle::display(void)
{
cout << “r = “ << radius << endl;
}
int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display();
}
20
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
}
r1 is statically allocated
width
length
r1
width = 5
length = 8
21
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
Rectangle *r2;
r2 = &r1;
r2->set(8,10);
}
r2 is a pointer to a Rectangle object
width
length
r1
width = 5
length = 8
5000
???
r2
6000
5000
width = 8
length =
10
//dot notation
//arrow notation
22
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
};
Declaration of an Object
main()
{
Rectangle *r3;
r3 = new Rectangle();
r3->set(80,100);
delete r3;
r3 = NULL;
}
r3 is dynamically allocated
???
r3
6000
width
length
5000
5000
width = 80
length = 100
???NULL
//arrow notation
23
#include <iostream.h>
class circle
{
public:
double radius;
};
Object Initialization
int main()
{
circle c1; // Declare an instance of the class circle
c1.radius = 5; // Initialize by assignment
}
1. By Assignment
• Only work for public data
members
• No control over the operations
on data members
24
#include <iostream.h>
class circle
{
private:
double radius;
public:
void set (double r)
{radius = r;}
double get_r ()
{return radius;}
};
int main(void) {
circle c; // an object of circle class
c.set(5.0); // initialize an object with a public member function
cout << "The radius of circle c is " << c.get_r() << endl;
// access a private data member with an accessor
}
Object Initialization
2. By Public Member Functions
25
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int
l);
int area();
}
Declaration of an Object
main()
{
Rectangle r1;
r1.set(5, 8);
Rectangle *r2;
r2 = &r1;
r2->set(8,10);
}
r2 is a pointer to a Rectangle object
//dot notation
//arrow notation
r1 and r2 are both initialized by
public member function set
26
class Rectangle
{
private:
int width;
int length;
public:
Rectangle();
Rectangle(const Rectangle
&r);
Rectangle(int w, int l);
void set(int w, int l);
int area();
}
Object Initialization
3. By Constructor
• Default constructor
• Copy constructor
• Constructor with parameters
There is no return type
Are used to initialize class data
members
Have the same name as the class
They are publicly accessible
They have different signatures
27
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
};
Object Initialization
• Default constructor
When a class is declared with no constructors,
the compiler automatically assumes default
constructor and copy constructor for it.
Rectangle :: Rectangle() { };
• Copy constructor
Rectangle :: Rectangle (const
Rectangle & r)
{
width = r.width; length = r.length;
};
28
class Rectangle
{
private:
int width;
int length;
public:
void set(int w, int l);
int area();
}
Object Initialization
• Initialize with default constructor
Rectangle r1;
Rectangle *r3 = new Rectangle();
• Initialize with copy constructor
Rectangle r4;
r4.set(60,80);
Rectangle r5 = r4;
Rectangle r6(r4);
Rectangle *r7 = new Rectangle(r4);
29
class Rectangle
{
private:
int width;
int length;
public:
Rectangle(int w, int l)
{width =w; length=l;}
void set(int w, int l);
int area();
}
Object Initialization
If any constructor with any number of
parameters is declared, no default
constructor will exist, unless you
define it.
Rectangle r4; // error
• Initialize with constructor
Rectangle r5(60,80);
Rectangle *r6 = new Rectangle(60,80);
30
class Rectangle
{
private:
int width;
int length;
public:
Rectangle();
Rectangle(int w, int l);
void set(int w, int l);
int area();
}
Object Initialization
Write your own constructors
Rectangle :: Rectangle()
{
width = 20;
length = 50;
};
Rectangle *r7 = new Rectangle();
width
length
width = 20
length = 50
5000???
r7
6000
5000
31
class Account
{
private:
char *name;
double balance;
unsigned int id;
public:
Account();
Account(const Account
&a);
Account(const char
*person);
}
Object Initialization
With constructors, we have more
control over the data members
Account :: Account()
{
name = NULL; balance = 0.0;
id = 0;
};
Account :: Account(const Account &a)
{
name = new char[strlen(a.name)+1];
strcpy (name, a.name);
balance = a.balance;
id = a.id;
};
Account :: Account(const char *person)
{
name = new char[strlen(person)+1];
strcpy (name, person);
balance = 0.0;
id = 0;
};
32
So far, …
• An object can be initialized by a class
constructor
– default constructor
– copy constructor
– constructor with parameters
• Resources are allocated when an object is
initialized
• Resources should be revoked when an
object is about to end its lifetime
33
Cleanup of An Object
class Account
{
private:
char *name;
double balance;
unsigned int id; //unique
public:
Account();
Account(const Account
&a);
Account(const char
*person);
~Account();
}
Destructor
Account :: ~Account()
{
delete[] name;
}
• Its name is the class name
preceded by a ~ (tilde)
• It has no argument
• It is used to release dynamically
allocated memory and to perform
other "cleanup" activities
• It is executed automatically when
the object goes out of scope
34
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
Str :: Str() {
pData = new char[1];
*pData = ‘0’;
nLength = 0;
};
Str :: Str(const Str &str) {
int n = str.nLength;
pData = new char[n+1];
nLength = n;
strcpy(pData,str.pData);
};
Str :: Str(char *s) {
pData = new
char[strlen(s)+1];
strcpy(pData, s);
nLength = strlen(s);
};
35
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
char* Str :: get_Data()
{
return pData;
};
Str :: ~Str()
{
delete[] pData;
};
int Str :: get_Len()
{
return nLength;
};
36
Putting Them Together
class Str
{
char *pData;
int nLength;
public:
//constructors
Str();
Str(char *s);
Str(const Str &str);
//accessors
char* get_Data();
int get_Len();
//destructor
~Str();
};
int main()
{
int x=3;
Str *pStr1 = new Str(“Joe”);
Str *pStr2 = new Str();
}
37
Interacting Objects
38
Working with Multiple Files
• To improve the readability, maintainability and
reusability, codes are organized into modules.
• When working with complicated codes,
– A set of .cpp and .h files for each class groups
• .h file contains the prototype of the class
• .cpp contains the definition/implementation of the class
– A .cpp file containing main() function, should include
all the corresponding .h files where the functions used
in .cpp file are defined
39
Example : time.h
// SPECIFICATION FILE ( time .h )
// Specifies the data members and
// member functions prototypes.
#ifndef _TIME_H
#define _TIME_H
class Time
{
public:
. . .
private:
. . .
} ;
#endif
40
// IMPLEMENTATION FILE ( time.cpp )
// Implements the member functions of class Time
#include <iostream.h>
#include “ time.h” // also must appear in client code
… …
bool Time :: Equal ( Time otherTime ) const
// Function value == true, if this time equals otherTime
// == false , otherwise
{
return ( (hrs == otherTime.hrs) && (mins == otherTime.mins)
&& (secs == otherTime.secs) ) ;
}
. . .
Example : time.cpp
41
Example : main.cpp
// Client Code ( main.cpp )
#include “ time.h”
// other functions, if any
int main()
{
… …
}
Compile and Run
g++ -o mainExec main.cpp time.cpp
42
Separate Compilation and Linking of Files
time.h
main.cpp time.cpp
main.o
mainExec
time.o
Compiler Compiler
Linker
#include “time.h”
implementation file
specification file
main program

More Related Content

What's hot

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
 
Testing for share
Testing for share Testing for share
Testing for share
Rajeev Mehta
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
Mihai Todor
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
Sasha Goldshtein
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
Hoang Nguyen
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
fawzmasood
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
Ilio Catallo
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
HalaiHansaika
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2Hariz Mustafa
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
tarun308
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
Intro C# Book
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
HelpWithAssignment.com
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
Olve Maudal
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
Diksha Bhargava
 
C# programming
C# programming C# programming
C# programming
umesh patil
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsFALLEE31188
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
David Beazley (Dabeaz LLC)
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 

What's hot (20)

Objective-C for iOS Application Development
Objective-C for iOS Application DevelopmentObjective-C for iOS Application Development
Objective-C for iOS Application Development
 
Testing for share
Testing for share Testing for share
Testing for share
 
Gentle introduction to modern C++
Gentle introduction to modern C++Gentle introduction to modern C++
Gentle introduction to modern C++
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Classes and data abstraction
Classes and data abstractionClasses and data abstraction
Classes and data abstraction
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
constructors and destructors in c++
constructors and destructors in c++constructors and destructors in c++
constructors and destructors in c++
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Ruby Programming Assignment Help
Ruby Programming Assignment HelpRuby Programming Assignment Help
Ruby Programming Assignment Help
 
Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
C# programming
C# programming C# programming
C# programming
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 
iOS Session-2
iOS Session-2iOS Session-2
iOS Session-2
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 

Similar to Classes and object

C
CC
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
HimanshuSharma997566
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
Dr. Md. Shohel Sayeed
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
Jay Patel
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Keyur Vadodariya
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
jehan1987
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
VGaneshKarthikeyan
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
VGaneshKarthikeyan
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
Intro C# Book
 
Review constdestr
Review constdestrReview constdestr
Review constdestrrajudasraju
 
The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202
Mahmoud Samir Fayed
 
C++ process new
C++ process newC++ process new
C++ process new
敬倫 林
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
saifnasir3
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
Dhivya Shanmugam
 
Function class in c++
Function class in c++Function class in c++
Function class in c++Kumar
 

Similar to Classes and object (20)

C
CC
C
 
Unit 2
Unit 2Unit 2
Unit 2
 
Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4Object Oriented Programming using C++ - Part 4
Object Oriented Programming using C++ - Part 4
 
C++_notes.pdf
C++_notes.pdfC++_notes.pdf
C++_notes.pdf
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Oops concept
Oops conceptOops concept
Oops concept
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
UNIT I (1).ppt
UNIT I (1).pptUNIT I (1).ppt
UNIT I (1).ppt
 
Writing MySQL UDFs
Writing MySQL UDFsWriting MySQL UDFs
Writing MySQL UDFs
 
14. Defining Classes
14. Defining Classes14. Defining Classes
14. Defining Classes
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
 
The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202The Ring programming language version 1.8 book - Part 86 of 202
The Ring programming language version 1.8 book - Part 86 of 202
 
C++ process new
C++ process newC++ process new
C++ process new
 
OOP.pptx
OOP.pptxOOP.pptx
OOP.pptx
 
Operator overload rr
Operator overload  rrOperator overload  rr
Operator overload rr
 
Function class in c++
Function class in c++Function class in c++
Function class in c++
 

More from Ankit Dubey

Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}
Ankit Dubey
 
Scheduling
Scheduling Scheduling
Scheduling
Ankit Dubey
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
Ankit Dubey
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
Ankit Dubey
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
Ankit Dubey
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
Ankit Dubey
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
Ankit Dubey
 
Ch5 cpu-scheduling
Ch5 cpu-schedulingCh5 cpu-scheduling
Ch5 cpu-scheduling
Ankit Dubey
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
Ankit Dubey
 
Ch3 processes
Ch3 processesCh3 processes
Ch3 processes
Ankit Dubey
 
Ch2 system structure
Ch2 system structureCh2 system structure
Ch2 system structure
Ankit Dubey
 
Ch1 introduction-to-os
Ch1 introduction-to-osCh1 introduction-to-os
Ch1 introduction-to-os
Ankit Dubey
 
Android i
Android iAndroid i
Android i
Ankit Dubey
 
Mongodb mock test_ii
Mongodb mock test_iiMongodb mock test_ii
Mongodb mock test_ii
Ankit Dubey
 
Android mock test_iii
Android mock test_iiiAndroid mock test_iii
Android mock test_iii
Ankit Dubey
 
Android mock test_ii
Android mock test_iiAndroid mock test_ii
Android mock test_ii
Ankit Dubey
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
Ankit Dubey
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
Ankit Dubey
 
Ajp notes-chapter-04
Ajp notes-chapter-04Ajp notes-chapter-04
Ajp notes-chapter-04
Ankit Dubey
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 

More from Ankit Dubey (20)

Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}Unit 1 android and it's tools quiz {mad cwipedia}
Unit 1 android and it's tools quiz {mad cwipedia}
 
Scheduling
Scheduling Scheduling
Scheduling
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Ch5 cpu-scheduling
Ch5 cpu-schedulingCh5 cpu-scheduling
Ch5 cpu-scheduling
 
Ch4 threads
Ch4 threadsCh4 threads
Ch4 threads
 
Ch3 processes
Ch3 processesCh3 processes
Ch3 processes
 
Ch2 system structure
Ch2 system structureCh2 system structure
Ch2 system structure
 
Ch1 introduction-to-os
Ch1 introduction-to-osCh1 introduction-to-os
Ch1 introduction-to-os
 
Android i
Android iAndroid i
Android i
 
Mongodb mock test_ii
Mongodb mock test_iiMongodb mock test_ii
Mongodb mock test_ii
 
Android mock test_iii
Android mock test_iiiAndroid mock test_iii
Android mock test_iii
 
Android mock test_ii
Android mock test_iiAndroid mock test_ii
Android mock test_ii
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
 
Ajp notes-chapter-05
Ajp notes-chapter-05Ajp notes-chapter-05
Ajp notes-chapter-05
 
Ajp notes-chapter-04
Ajp notes-chapter-04Ajp notes-chapter-04
Ajp notes-chapter-04
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 

Recently uploaded

14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
azkamurat
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
obonagu
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
dxobcob
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 

Recently uploaded (20)

14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Ethernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.pptEthernet Routing and switching chapter 1.ppt
Ethernet Routing and switching chapter 1.ppt
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
原版制作(unimelb毕业证书)墨尔本大学毕业证Offer一模一样
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
一比一原版(Otago毕业证)奥塔哥大学毕业证成绩单如何办理
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 

Classes and object

  • 1. 1 • A pointer variable is a variable whose value is the address of a location in memory int x; x = 5; int* ptr1; ptr1 = &x; int* ptr2; ptr2 = ptr1; *ptr1 = 6; cout << ptr1 << endl; cout << *ptr2 << endl; Review: Pointers & Dynamic Data int* ptr3; ptr3 = new int; *ptr3 = 5; delete ptr3; ptr3 = NULL; int *ptr4; ptr4 = new int[5]; ptr4[0] = 100; ptr4[4] = 123; delete [] ptr4; ptr4 = NULL;
  • 2. 2 void increment(int b1, int &b2, int *b3) { b1 += 2; b2 += 2 *b3 += 2; } Review: Reference Types • Reference Types – Alias for another variable – Must be initialized when declared – Are primarily used as function parameters int main (void){ int a1 = 5, a2 = 10; int *a3 = new int; *a3 = 15; int &a4 = a3; cout << a1 << a2 << a3 << endl; increment(a1, a2, a3); cout << a1 << a2 << a3 << endl; delete a3; a3 = NULL; return 0; }
  • 3. 3 Object-Oriented Programming Introduction to Classes • Class Definition • Class Examples • Objects • Constructors • Destructors
  • 4. 4 Class • The class is the cornerstone of C++ – It makes possible encapsulation, data hiding and inheritance • Type – Concrete representation of a concept • Eg. float with operations like -, *, + (math real numbers) • Class – A user defined type – Consists of both data and methods – Defines properties and behavior of that type • Advantages – Types matching program concepts • Game Program (Explosion type) – Concise program – Code analysis easy – Compiler can detect illegal uses of types • Data Abstraction – Separate the implementation details from its essential properties
  • 5. 5 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Classes & Objects Rectangle r1; Rectangle r2; Rectangle r3; …… int a; Objects: Instance of a class
  • 6. 6 Define a Class Type class class_name { permission_label: member; permission_label: member; ... }; class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Body Header
  • 7. 7 Class Definition Data Members • Can be of any type, built-in or user-defined • non-static data member – Each class object has its own copy • static data member – Acts as a global variable – One copy per class type, e.g. counter
  • 8. 8 class Rectangle { private: int width; int length; static int count; public: void set(int w, int l); int area(); } Static Data Member Rectangle r1; Rectangle r2; Rectangle r3; width length width length width length r1 r3 r2 count
  • 9. 9 Class Definition Member Functions • Used to – access the values of the data members (accessor) – perform operations on the data members (implementor) • Are declared inside the class body • Their definition can be placed inside the class body, or outside the class body • Can access both public and private members of the class • Can be referred to using dot or arrow member access operator
  • 10. 10 Define a Member Function class Rectangle { private: int width, length; public: void set (int w, int l); int area() {return width*length; } }; void Rectangle :: set (int w, int l) { width = w; length = l; } inline class name member function name scope operator r1.set(5,8); rp->set(8,10);
  • 11. 11 • const member function – declaration • return_type func_name (para_list) const; – definition • return_type func_name (para_list) const { … } • return_type class_name :: func_name (para_list) const { … } – Makes no modification about the data members (safe function) – It is illegal for a const member function to modify a class data member Class Definition Member Functions
  • 12. 12 Const Member Function class Time { private : int hrs, mins, secs ; public : void Write ( ) const ; } ; void Time :: Write( ) const { cout <<hrs << “:” << mins << “:” << secs << endl; } function declaration function definition
  • 13. 13 • Information hiding – To prevent the internal representation from direct access from outside the class • Access Specifiers – public • may be accessible from anywhere within a program – private • may be accessed only by the member functions, and friends of this class – protected • acts as public for derived classes • behaves as private for the rest of the program Class Definition - Access Control
  • 14. 14 class Time Specification class Time { public : void Set ( int hours , int minutes , int seconds ) ; void Increment ( ) ; void Write ( ) const ; Time ( int initHrs, int initMins, int initSecs ) ; // constructor Time ( ) ; // default constructor private : int hrs ; int mins ; int secs ; } ; 14
  • 15. 15 Class Interface Diagram Private data: hrs mins secs Set Increment Write Time Time Time class
  • 16. 16 • The default access specifier is private • The data members are usually private or protected • A private member function is a helper, may only be accessed by another member function of the same class (exception friend function) • The public member functions are part of the class interface • Each access control section is optional, repeatable, and sections may occur in any order Class Definition Access Control
  • 17. 17 What is an object? OBJECT Operations Data set of methods (member functions) internal state (values of private data members)
  • 18. 18 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Declaration of an Object main() { Rectangle r1; Rectangle r2; r1.set(5, 8); cout<<r1.area()<<endl; r2.set(8,10); cout<<r2.area()<<endl; }
  • 19. 19 Another Example #include <iostream.h> class circle { private: double radius; public: void store(double); double area(void); void display(void); }; // member function definitions void circle::store(double r) { radius = r; } double circle::area(void) { return 3.14*radius*radius; } void circle::display(void) { cout << “r = “ << radius << endl; } int main(void) { circle c; // an object of circle class c.store(5.0); cout << "The area of circle c is " << c.area() << endl; c.display(); }
  • 20. 20 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Declaration of an Object main() { Rectangle r1; r1.set(5, 8); } r1 is statically allocated width length r1 width = 5 length = 8
  • 21. 21 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Declaration of an Object main() { Rectangle r1; r1.set(5, 8); Rectangle *r2; r2 = &r1; r2->set(8,10); } r2 is a pointer to a Rectangle object width length r1 width = 5 length = 8 5000 ??? r2 6000 5000 width = 8 length = 10 //dot notation //arrow notation
  • 22. 22 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Declaration of an Object main() { Rectangle *r3; r3 = new Rectangle(); r3->set(80,100); delete r3; r3 = NULL; } r3 is dynamically allocated ??? r3 6000 width length 5000 5000 width = 80 length = 100 ???NULL //arrow notation
  • 23. 23 #include <iostream.h> class circle { public: double radius; }; Object Initialization int main() { circle c1; // Declare an instance of the class circle c1.radius = 5; // Initialize by assignment } 1. By Assignment • Only work for public data members • No control over the operations on data members
  • 24. 24 #include <iostream.h> class circle { private: double radius; public: void set (double r) {radius = r;} double get_r () {return radius;} }; int main(void) { circle c; // an object of circle class c.set(5.0); // initialize an object with a public member function cout << "The radius of circle c is " << c.get_r() << endl; // access a private data member with an accessor } Object Initialization 2. By Public Member Functions
  • 25. 25 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Declaration of an Object main() { Rectangle r1; r1.set(5, 8); Rectangle *r2; r2 = &r1; r2->set(8,10); } r2 is a pointer to a Rectangle object //dot notation //arrow notation r1 and r2 are both initialized by public member function set
  • 26. 26 class Rectangle { private: int width; int length; public: Rectangle(); Rectangle(const Rectangle &r); Rectangle(int w, int l); void set(int w, int l); int area(); } Object Initialization 3. By Constructor • Default constructor • Copy constructor • Constructor with parameters There is no return type Are used to initialize class data members Have the same name as the class They are publicly accessible They have different signatures
  • 27. 27 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); }; Object Initialization • Default constructor When a class is declared with no constructors, the compiler automatically assumes default constructor and copy constructor for it. Rectangle :: Rectangle() { }; • Copy constructor Rectangle :: Rectangle (const Rectangle & r) { width = r.width; length = r.length; };
  • 28. 28 class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } Object Initialization • Initialize with default constructor Rectangle r1; Rectangle *r3 = new Rectangle(); • Initialize with copy constructor Rectangle r4; r4.set(60,80); Rectangle r5 = r4; Rectangle r6(r4); Rectangle *r7 = new Rectangle(r4);
  • 29. 29 class Rectangle { private: int width; int length; public: Rectangle(int w, int l) {width =w; length=l;} void set(int w, int l); int area(); } Object Initialization If any constructor with any number of parameters is declared, no default constructor will exist, unless you define it. Rectangle r4; // error • Initialize with constructor Rectangle r5(60,80); Rectangle *r6 = new Rectangle(60,80);
  • 30. 30 class Rectangle { private: int width; int length; public: Rectangle(); Rectangle(int w, int l); void set(int w, int l); int area(); } Object Initialization Write your own constructors Rectangle :: Rectangle() { width = 20; length = 50; }; Rectangle *r7 = new Rectangle(); width length width = 20 length = 50 5000??? r7 6000 5000
  • 31. 31 class Account { private: char *name; double balance; unsigned int id; public: Account(); Account(const Account &a); Account(const char *person); } Object Initialization With constructors, we have more control over the data members Account :: Account() { name = NULL; balance = 0.0; id = 0; }; Account :: Account(const Account &a) { name = new char[strlen(a.name)+1]; strcpy (name, a.name); balance = a.balance; id = a.id; }; Account :: Account(const char *person) { name = new char[strlen(person)+1]; strcpy (name, person); balance = 0.0; id = 0; };
  • 32. 32 So far, … • An object can be initialized by a class constructor – default constructor – copy constructor – constructor with parameters • Resources are allocated when an object is initialized • Resources should be revoked when an object is about to end its lifetime
  • 33. 33 Cleanup of An Object class Account { private: char *name; double balance; unsigned int id; //unique public: Account(); Account(const Account &a); Account(const char *person); ~Account(); } Destructor Account :: ~Account() { delete[] name; } • Its name is the class name preceded by a ~ (tilde) • It has no argument • It is used to release dynamically allocated memory and to perform other "cleanup" activities • It is executed automatically when the object goes out of scope
  • 34. 34 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; Str :: Str() { pData = new char[1]; *pData = ‘0’; nLength = 0; }; Str :: Str(const Str &str) { int n = str.nLength; pData = new char[n+1]; nLength = n; strcpy(pData,str.pData); }; Str :: Str(char *s) { pData = new char[strlen(s)+1]; strcpy(pData, s); nLength = strlen(s); };
  • 35. 35 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; char* Str :: get_Data() { return pData; }; Str :: ~Str() { delete[] pData; }; int Str :: get_Len() { return nLength; };
  • 36. 36 Putting Them Together class Str { char *pData; int nLength; public: //constructors Str(); Str(char *s); Str(const Str &str); //accessors char* get_Data(); int get_Len(); //destructor ~Str(); }; int main() { int x=3; Str *pStr1 = new Str(“Joe”); Str *pStr2 = new Str(); }
  • 38. 38 Working with Multiple Files • To improve the readability, maintainability and reusability, codes are organized into modules. • When working with complicated codes, – A set of .cpp and .h files for each class groups • .h file contains the prototype of the class • .cpp contains the definition/implementation of the class – A .cpp file containing main() function, should include all the corresponding .h files where the functions used in .cpp file are defined
  • 39. 39 Example : time.h // SPECIFICATION FILE ( time .h ) // Specifies the data members and // member functions prototypes. #ifndef _TIME_H #define _TIME_H class Time { public: . . . private: . . . } ; #endif
  • 40. 40 // IMPLEMENTATION FILE ( time.cpp ) // Implements the member functions of class Time #include <iostream.h> #include “ time.h” // also must appear in client code … … bool Time :: Equal ( Time otherTime ) const // Function value == true, if this time equals otherTime // == false , otherwise { return ( (hrs == otherTime.hrs) && (mins == otherTime.mins) && (secs == otherTime.secs) ) ; } . . . Example : time.cpp
  • 41. 41 Example : main.cpp // Client Code ( main.cpp ) #include “ time.h” // other functions, if any int main() { … … } Compile and Run g++ -o mainExec main.cpp time.cpp
  • 42. 42 Separate Compilation and Linking of Files time.h main.cpp time.cpp main.o mainExec time.o Compiler Compiler Linker #include “time.h” implementation file specification file main program