0% found this document useful (0 votes)
23 views10 pages

Class ١

Uploaded by

Youssef Sameh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views10 pages

Class ١

Uploaded by

Youssef Sameh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

structs that only contain variables represent the traditional non-object-

oriented programming world, as they can only hold data.


In the world of object-oriented programming, we often want our types to not
only hold data, but provide functions that work with the data as well.
In C++, this is typically done via the .
keyword defines a new user-defined type.

struct

class
the only significant difference is the public keyword in the class.
declaration does not declare any memory. It only defines what
the class looks like

DateClass today { 2020, 10, 14 }; // declare a variable of class DateClass

Defining a variable of a class we call it instantiating the class.


The variable is called an instance, of the class
A variable of a class type is also called an object.
Instantiating an object (e.g. DateClass today) allocates memory for that object.

Classes can also contain functions.


Functions defined inside of a class are
called member functions ( methods).
Member functions can be defined inside or
outside of the class definition.
When we call “today.print()”, we’re telling the
compiler to call the print() member function,
associated with the today object.

Function member
Using the “m_” prefix for member
variables helps distinguish member
variables from function parameters or
local variables inside member functions.
when we see an assignment to a
variable with the “m_” prefix  we are
changing the state of the class.
Unlike function parameters or local
variables, which are declared within the
function, member variables are
declared
in the class definition.
Rule!!!! Name your classes starting with
a capital letter.
A class that allocates memory will deallocate it before
being destroyed), but it’s not safe to assume a struct will.
we recommend using the struct keyword for data-only
structures, and the class keyword for defining objects that
require both data and functions to be bundled together.

Classes form the basis for Object-oriented programming


Access specifiers determine who has
access to the members that follow the
specifier.

1 3
2
public protecte
private d
Public members are members of a struct or class that can be accessed from outside of
the struct or class.
Private members are members of a class that can only be accessed by other
members of the class.
All members of a struct are public members by default.
All members of a class are private members by
default.

error!!!
In general, member variables are usually made private, and member functions are usually
made public.
base and height

You might also like