Lecture 3
CSE1201: Object Oriented
Programming-I (C++)
9/5/2023 2
Paradigm: Multi-paradigm
Creator: Bjarne Stroustrup
First appeared: 1983
C++ Program Execution Flow
9/5/2023 3
A First C++ Program
#include <iostream>
using namespace std;
// main() is where program execution begins
int main() {
// prints Hello World
cout << "Hello World";
return 0;
}
9/5/2023 4
A First C++ Program
• Save the program as first.cpp
• Build and Run the code
9/5/2023 5
Another C++ Program
#include <iostream>
using namespace std;
int main() {
int num;
cin >> num;
cout << "number: " <<num << endl;
return 0;
}
9/5/2023 6
The General Form of a Class in C++
 A class is defined by specifying the data and the
code that operate on the data
 The general form:
class classname{
private data and functions declarations
access-specifier:
data and functions declarations
access-specifier:
data and functions declarations
};
7
9/5/2023
Example
class person
{
int age;
public:
void display_age()
{
cout << age;
}
};
The General Form of a Class in C++
• Functions that are declared within a class are
called member functions
• Member functions may access any element of the
class of which they are a part, this includes all
private elements
• Variables that are elements of a class are called
member variables or data members. (The term
instance variable is also used)
9
9/5/2023
Access specifier
• access-specifier is one of these three C++ keywords:
– public
– private
– Protected
• (access-specifier is also known as visibility label)
• By default, functions and data declared within a class
are private to that class and may be accessed only by
other members of the class
9/5/2023 10
Access specifier
 The public access-specifier allows functions or data
to be accessible to other parts of your program
 The protected access-specifier is needed only when
inheritance is involved
 Once an access-specifier has been used, it remains in
effect until either another access-specifier is
encountered or the end of the class declaration is
reached
11
Access specifier
• In general, you should make all data members of a
class private to that class.
– This is part of the way that encapsulation is achieved
• However, there may be situations in which you will
need to make one or more variables public
12
Creating Objects
 Once a class has been declared, we can create
variables (object) of that type by using the class
name (like any other built in variables)
 The necessary memory space is allocated to an
object at this stage
 Class specification provides only a template and
does not create any memory space for the members
 Format: class_name object_name;
 Example: person p;
13
9/5/2023
Creating Objects
 Objects can also be created when a class is defined
by placing their names immediately after the closing
brace.
class class_name
{
} x, y, z;
 Each object contains its own copy of each instance
variables defined by the class
14
9/5/2023
Accessing class members
 The private data of a class can only be
accessed only through the member functions
of that class
 Following is the format for accessing a
member
Object_name.function_name(arguments)
Object_name.variable_name
9/5/2023 15
Defining member functions
 Member functions can be defined in two
places:
 Outside the class definition
 Inside the class definition
 Irrespective of the place of definition, the
function will perform the same task
9/5/2023 16
Outside the class definition
 Member functions that are only declared inside of a
class, have to be defined separately outside the class
 General format of a member function definition outside
the class is
return_type class_name:: function_name (arguments)
{
}
 The membership label class_name:: tells the compiler
that, function function_name belongs to the class
class_name
 The symbol :: is called the scope resolution operator
9/5/2023 17
Inside the class definition
 Another method of defining a member function is to
replace the function declaration by the actual
function definition inside the class definition
 Usually, only small functions are defined inside the
class definition
9/5/2023 18
General structure of a c++ program
Include files
Class declarations
Member functions definitions
Main function program
9/5/2023 19
Thank you

lecture3.pptx

  • 1.
    Lecture 3 CSE1201: ObjectOriented Programming-I (C++)
  • 2.
    9/5/2023 2 Paradigm: Multi-paradigm Creator:Bjarne Stroustrup First appeared: 1983
  • 3.
    C++ Program ExecutionFlow 9/5/2023 3
  • 4.
    A First C++Program #include <iostream> using namespace std; // main() is where program execution begins int main() { // prints Hello World cout << "Hello World"; return 0; } 9/5/2023 4
  • 5.
    A First C++Program • Save the program as first.cpp • Build and Run the code 9/5/2023 5
  • 6.
    Another C++ Program #include<iostream> using namespace std; int main() { int num; cin >> num; cout << "number: " <<num << endl; return 0; } 9/5/2023 6
  • 7.
    The General Formof a Class in C++  A class is defined by specifying the data and the code that operate on the data  The general form: class classname{ private data and functions declarations access-specifier: data and functions declarations access-specifier: data and functions declarations }; 7 9/5/2023
  • 8.
    Example class person { int age; public: voiddisplay_age() { cout << age; } };
  • 9.
    The General Formof a Class in C++ • Functions that are declared within a class are called member functions • Member functions may access any element of the class of which they are a part, this includes all private elements • Variables that are elements of a class are called member variables or data members. (The term instance variable is also used) 9 9/5/2023
  • 10.
    Access specifier • access-specifieris one of these three C++ keywords: – public – private – Protected • (access-specifier is also known as visibility label) • By default, functions and data declared within a class are private to that class and may be accessed only by other members of the class 9/5/2023 10
  • 11.
    Access specifier  Thepublic access-specifier allows functions or data to be accessible to other parts of your program  The protected access-specifier is needed only when inheritance is involved  Once an access-specifier has been used, it remains in effect until either another access-specifier is encountered or the end of the class declaration is reached 11
  • 12.
    Access specifier • Ingeneral, you should make all data members of a class private to that class. – This is part of the way that encapsulation is achieved • However, there may be situations in which you will need to make one or more variables public 12
  • 13.
    Creating Objects  Oncea class has been declared, we can create variables (object) of that type by using the class name (like any other built in variables)  The necessary memory space is allocated to an object at this stage  Class specification provides only a template and does not create any memory space for the members  Format: class_name object_name;  Example: person p; 13 9/5/2023
  • 14.
    Creating Objects  Objectscan also be created when a class is defined by placing their names immediately after the closing brace. class class_name { } x, y, z;  Each object contains its own copy of each instance variables defined by the class 14 9/5/2023
  • 15.
    Accessing class members The private data of a class can only be accessed only through the member functions of that class  Following is the format for accessing a member Object_name.function_name(arguments) Object_name.variable_name 9/5/2023 15
  • 16.
    Defining member functions Member functions can be defined in two places:  Outside the class definition  Inside the class definition  Irrespective of the place of definition, the function will perform the same task 9/5/2023 16
  • 17.
    Outside the classdefinition  Member functions that are only declared inside of a class, have to be defined separately outside the class  General format of a member function definition outside the class is return_type class_name:: function_name (arguments) { }  The membership label class_name:: tells the compiler that, function function_name belongs to the class class_name  The symbol :: is called the scope resolution operator 9/5/2023 17
  • 18.
    Inside the classdefinition  Another method of defining a member function is to replace the function declaration by the actual function definition inside the class definition  Usually, only small functions are defined inside the class definition 9/5/2023 18
  • 19.
    General structure ofa c++ program Include files Class declarations Member functions definitions Main function program 9/5/2023 19
  • 20.