0% found this document useful (0 votes)
32 views

Koya Technical Institute Object Oriented Program: K.T.I 2016-2017 by M.Wshyar

The document discusses key concepts in object-oriented programming in C++ including constants, preprocessor definitions, functions, classes, and access specifiers like private and public. It provides examples of declaring constants, defining functions, creating a basic class with data members and member functions, and specifying whether class members are private or public. The document serves as lecture notes for an introduction to OOP concepts in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Koya Technical Institute Object Oriented Program: K.T.I 2016-2017 by M.Wshyar

The document discusses key concepts in object-oriented programming in C++ including constants, preprocessor definitions, functions, classes, and access specifiers like private and public. It provides examples of declaring constants, defining functions, creating a basic class with data members and member functions, and specifying whether class members are private or public. The document serves as lecture notes for an introduction to OOP concepts in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Koya Technical Institute

Object Oriented Program

Lecture 2
Oct 26, 2017

1
K.T.I 2016-2017 By M.Wshyar
What is a Constants?
- Constants are expressions with a fixed value.

- Declared constants (const):


- With the const prefix you can declare constants
with a specific type in the same way as you would
do with a variable:

const int p = 100;


const char t = '\t';

Here, p and t are two typed constants. They are


treated just like regular variables except that their
values cannot be modified after their definition

2
K.T.I 2017-2018 By M.Wshyar
Example

3
K.T.I 2017-2018 By M.Wshyar
Preprocessor definitions (#define)

Note : The #define lines are


preprocessor directives, do not
require semicolons (;) at the
end;

4
K.T.I 2017-2018 By M.Wshyar
Example

5
K.T.I 2017-2018 By M.Wshyar
Functions
- In C++, a function is a group of statements that is
given a name, and which can be called from some
point of the program.
- The most common syntax to define a function is:
type name ( parameter1, parameter2, ...)
{ statements }

type is the type of the value returned by the


function.
name is the identifier.
parameters Each parameter consists of a type
followed by an identifier.
statements is the function's body. It is a block of
statements surrounded by braces { } that specify
6
what the function actually does. K.T.I 2017-2018 By M.Wshyar
Function Example

In this example, main


begins by declaring the
variable z of type int, and
right after that, it performs
the first function call: it calls
addition. In the example
above, the call to addition
can be compared to its
definition just a few lines
earlier:

7
K.T.I 2017-2018 By M.Wshyar
Function Example

8
K.T.I 2017-2018 By M.Wshyar
Function
- Multi function:

9
K.T.I 2017-2018 By M.Wshyar
What is a Class?
- A class is a blueprint for the object.
- Before you create an object in C++, you need
to define a class.
- We can think of class as a sketch
(prototype) of a house. It contains all the
details about the floors, doors, windows etc.
Based on these descriptions we build the
house. House is the object.
- As, many houses can be made from the
same description, we can create many
objects from a class.

10
K.T.I 2017-2018 By M.Wshyar
How to define a class in C++?
- A class is defined in C++ using keyword class
followed by the name of class.
- The body of class is defined inside the curly
brackets and terminated by a semicolon at the
end.

class className
{
// some data
// some functions
};

11
K.T.I 2017-2018 By M.Wshyar
Example: Class in C++

- Here, we defined a class


named Test.
- This class has two data members: 
- data1 and data2 and two member
functions: 
- function1()and function2().

12
K.T.I 2017-2018 By M.Wshyar
private and public in the above example.
- The private keyword makes data and functions
private. Private data and functions can be
accessed only from inside the same class.
- The public keyword makes data and functions
public. Public data and functions can be accessed
out of the class.

- Here, data1 and data2 are private members


where as function1() and function2() are public
members.
- If you try to access private data from outside of
the class, compiler throws error. This feature
in OOP is known as data hiding.
13
K.T.I 2017-2018 By M.Wshyar
14

You might also like