C++ was originally developed as an extension of C to add object-oriented capabilities. While C++ retains much of C's syntax and functionality, it introduces important new concepts like classes, objects, and inheritance. A class defines the common properties and behaviors (methods) that objects of that class will have. Objects are instances of classes that allocate memory at runtime. This allows C++ to support object-oriented programming by encapsulating data into objects that can receive messages.
1. Recall
⢠What are files handling used for?
⢠How do we write in to a file?
⢠What are the different modes of opening a
file?
⢠What is fgetc() ?
3. C++
⢠Super Set of C
C++ was originally developed to be the next version of C, not a new
language.
⢠Backward compactable with C
Most of the C functions can run in C++
⢠Same compiler preprocessor
Must have a function names âmainâ to determine where the program starts
6. C Vs C++
⢠int
⢠Char
⢠Float
⢠double
âExactly the same as
of left sideâ
Data Types
7. C Vs C++
⢠Conditional control
structures
â If
â If else
â Switch
⢠Loops
â for
â while
â Do while
âExactly the same as
of left sideâ
Control Structures
8. C Vs C++
int sum(int a, int b)
{
Int c;
c=a + b
return c;
}
sum(12,13)
âExactly the same as
of left sideâ
functions
9. C Vs C++
Int a[10];
Int b[] = {1000, 2, 3,
50}; âExactly the same as
of left sideâ
Arrays
11. C Vs C++
Output
printf(â value of a = %dâ,a);
Printf(âa = %d and b= %dâ,a,b);
Input
scanf(â%d ", &a);
Scanf(â%dâ,&a,&b);
Output
cout<< âvalue of a =" << a;
Cout<<âa =â<<a<<âb=â<<b;
Input
cin>>a;
Cin>>a>>b;;
Input / Output
13. C Vs C++
struct Data
{
int x;
};
struct Data module;
âmodule.x = 5;
struct Data
{
int x;
void printMe()
{
cout<<x;
}
} Data;
Data module,Module2;
module.x = 5;
module2.x = 12;
module.printMe() // Prints 5
module.printMe() // Prints 12
Structures
16. OOP Concept
⢠Object-oriented programming (OOP) is a
style of programming that focuses on
using objects to design and build
applications.
⢠Think of an object as a model of the
concepts, processes, or things in the real
19. Objects in real world
⢠Object will have an identity/name
ď§ Eg: reynolds, Cello for pen. Nokia,apple for
mobile
⢠Object will have different properties which
describes them best
ď§ Eg:Color,size,width
⢠Object can perform different actions
ď§ Eg: writing,erasing etc for pen. Calling,
20. Objects
I have an identity:
I'm Volkswagen
I have different properties.
My color property is green
My no:of wheel property is 4
I can perform different actions
I can be drived
I can consume fuel
I can play Music
I have an identity:
I'm Suzuki
I have different properties.
My color property is silver
My no:of wheel property is 4
I can perform different actions
I can be drived
I can consume fuel
I can play Music
21. How these objects are created?
⢠All the objects in the real world are created
out of a basic prototype or a basic blue
print or a base design
23. Objects in the software world
⢠Same like in the real world we can create
objects in computer programming world
âWhich will have a name as identity
âProperties to define its behaviour
âActions what it can perform
24. How these objects are
created
⢠We need to create a base design which
defines the properties and functionalities
that the object should have.
⢠In programming terms we call this base
design as Class
⢠Simply by having a class we can create
any number of objects of that type
25. Definition
⢠Class : is the base design of
objects
⢠Object : is the instance of a class
⢠No memory is allocated when a class is
created.
⢠Memory is allocated only when an object is
created.
26. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
public:
27. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the Keyword to create
any class
public:
28. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the name of the class
public:
29. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Called access specifier.
Will detailed soonpublic:
30. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Are two variable that
referred as the
properties
public:
31. How to create class in
C++
class shape
{
Int width;
Int height;
Int calculateArea()
{
return x*y
}
}
Is the only functionality
of this class
public:
32. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Is the class name
33. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Two objects of base type
shape
34. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Setting properties of
object named rectangle
35. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Setting properties of
object named square
36. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Calling the functionality
of rectangle which is
claclulateArea();
37. How to create objects in
C++
shape rectangle,square;
rectangle.width=20;
recangle.height=35;
square.height=10;
square.width=10;
rArea=rectangle.calculateArea();
sArea=square.calculateArea();
Calling the functionality
of rectangle which is
claclulateArea();
38. Example
Class : shape
Height:35
width:20
Object rectangle
calculateArea()
{
Return 20*35
}
Height:10
width:10
Object square
calculateArea()
{
Return 10*10;
}
Member variables
Height
width
Member function
calculateArea
{
return height*width;
}
39. Access Specifier
⢠Access specifiers defines the access
rights for the statements or functions that
follows it until another access specifier or
till the end of a class.
⢠The three types of access specifiers are
âPrivate
âPublic
âProtected
41. Class Vs Structure
⢠Class is similar to Structure.
⢠Structure members have public access by
default.
⢠Class members have private access by
default.
43. Self Check
⢠Which of the following term is used for a
function defined inside a class?
âMember Variable
âMember function
âClass function
âClassic function
44. Self Check
⢠Which of the following term is used for a
function defined inside a class?
âMember Variable
âMember function
âClass function
âClassic function
47. Self Check
⢠Which of the following is correct about
class and structure?
â class can have member functions while
structure cannot.
â class data members are public by default
while that of structure are private.
â Pointer to structure or classes cannot be
declared.
â class data members are private by default
while that of structure are public by default.
48. Self Check
⢠Which of the following is correct about
class and structure?
â class can have member functions while
structure cannot.
â class data members are public by default
while that of structure are private.
â Pointer to structure or classes cannot be
declared.
â class data members are private by default
while that of structure are public by default.
49. Self Check
⢠Which of the following operator is
overloaded for object cout?
â˘>>
â˘<<
â˘+
â˘=
50. Self Check
⢠Which of the following operator is
overloaded for object cout?
â˘>>
â˘<<
â˘+
â˘=
51. Self Check
⢠Which of the following access
specifier is used as a default in a
class definition?
â˘protected
â˘public
â˘private
â˘friend
52. Self Check
⢠Which of the following access
specifier is used as a default in a
class definition?
â˘protected
â˘public
â˘private
â˘friend