SlideShare a Scribd company logo
Module 5
Overview of Object Oriented Prgg.
Features of OOP - Classes and Objects - “this” pointer - Constructors
and Destructors - Static Data Members, Static Member Functions
and Objects - Inline Functions – Call by reference - Functions with
default Arguments - Functions with Objects as Arguments - Friend
Functions and Friend Classes.
#include <stdio.h>
int main()
{
int num1, num2, sum;
scanf("%d", &num1);
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum:%dn", sum);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"Sum: "<<sum;
return 0;
}
C C++
#include <stdio.h>
int main()
{
int num1, num2, sum;
scanf("%d", &num1);
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum:%dn", sum);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"Sum: "<<sum;
return 0;
}
C++ compiler can execute both programs
The key differences between old-style and modern code involve two features:
new-style headers and the namespace statement.
#include <iostream>
int main()
{
int num1, num2, sum;
std::cin >> num1;
std::cin >> num2;
sum = num1 + num2;
std::cout << "Sum: " << sum;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum;
cin>>num1;
cin>>num2;
sum = num1 + num2;
cout<<"Sum: "<<sum;
return 0;
}
With namespace Without namespace
namespace
• a namespace is a collection of related names or identifiers (functions, class,
variables) which helps to separate these identifiers from similar identifiers in
other namespaces or the global namespace.
• Namespaces are a recent addition to C++.
• A namespace creates a declarative region in which various program
elements can be placed.
• The using statement informs the compiler that you want to use the std
namespace.
• This is the namespace in which the entire Standard C++ library is declared.
• By using the std namespace you simplify access to the standard library
• Standard C++ created a new kind of header that is used by the Standard
C++ library.
• The new-style headers do not specify filenames.
• Instead, they simply specify standard identifiers that may be mapped to files
by the compiler, although they need not be.
• The new-style C++ headers are an abstraction that simply guarantee that
the appropriate prototypes and definitions required by the C++ library have
been declared.
• Since the new-style headers are not filenames, they do not have a .h
extension.
• They consist solely of the header name contained between angle brackets.
• The C++ versions of the C standard headers simply add a "c" prefix to the
filename and drop the .h. For example, the C++ new-style header for
math.h is cmath. The one for string.h is cstring.
Defining a class Accessing class members
from outside function
Object creation:
Class_name Object_name;
Object_name.function_name();
Object_name.variable_name;
Defining a class class members can be directly
accessed by its member function
function_name();
variable_name;
#include <iostream>
using namespace std;
class Student {
private:
int rollno;
float percentage;
public:
// Function to get input
void get() {
cout << "Enter details: ";
cin >> rollno;
cin >> percentage;
}
C++ prg with Class and object
// Function to display output
void put() {
cout << "nStudent Details:n";
cout << rollno << “n”<<percentage;
}
};
int main() {
Student s1; // Create an object of Student class
s1.get(); // Call get() to take input
s1.put(); // Call put() to display output
return 0;
}
• 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.
• 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.
Access specifier
• You may change access specifications as often as you like within a class
declaration.
Access specifier
#include <iostream>
using namespace std;
class Student {
private:
int rollno;
float percentage;
public:
// Function to get input
void get() {
cout << "Enter details: ";
cin >> rollno;
cin >> percentage;
}
// Function to display output
void put() {
cout << "nStudent Details:n";
cout << rollno << “n”<<percentage;
}
};
int main() {
Student s1;
s1.get();
s1.put();
s1.rollno=22;
return 0;
}
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
void get()
{
cout << "Enter details: ";
cin >> rollno;
}
void put()
{
cout << "nStudent Details:n";
cout << rollno;
}
};
int main() {
Student s1;
s1.get();
s1.put();
return 0;
}
Structure and class
Members of a class
• Elements of a class.
• 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.)
Characteristics of member functions:
• Several different classes can use the same function name. The
'membership label' will resolve their scope.
• Member functions can access the private data of the class. A non-
member function cannot do so.
• A member function can call another member function directly, without
using the dot operator.
Defining member functions
Inside the class definition
Outside the class definition
Defining member functions
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get()
{ cin >> rollno; }
void put()
{ cout << rollno; }
};
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get();
void put();
};
void Student :: get()
{ cin >> rollno; }
void Student :: put()
{ cout << rollno; }
Nesting of member functions
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get()
{ cin >> rollno;
put();
}
void put()
{ cout << rollno; }
};
int main()
{
Student s1;
s1.get();
return 0;
}
Member function with args
Member function with default args
member function with args
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get(int a)
{ rollno = a; }
void put()
{ cout << rollno; }
};
int main()
{
Student s1;
s1.get(20);
s1.put();
return 0;
}
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
Valid valid
Invalid invalid
Function with default args
• C++ allows us to call a function without specifying all its arguments.
• In such cases, function assigns a default value to the parameter which does
not have a matching argument in the function call.
• Default values are specified when the function is declared.
• The compiler looks at the prototype to see how many arguments a function
uses and alerts the program for possible default values.
• A default argument is checked for type at the time of declaration and
evaluated at the time of call.
• One important point to note is that only the trailing arguments can have
default values and therefore we must add defaults from right to left.
• We cannot provide a default value to a particular argument in the middle
of an argument list.
• Some examples of function declaration with default values are:
• int mul(int i, int j=5, int k=10);
• int mul(int i=5, int j);
• int mul(int i=0, int j, int k=10);
• int mul(int i=2, int j=5, int k=10);
• // legal
• // illegal
• // illegal
• // legal
Function with default args
• Default arguments are useful in situations where some arguments always
have the same value. For instance, bank interest may remain the same for
all customers for a particular period of deposit. It also provides a greater
flexibility to the programmers. A function can be written with more
parameters than are required for its most common application. Using
default arguments, a programmer can use only those arguments that are
meaningful to a particular situation.
Function with default args
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
member function with default args
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get(int a=5)
{ rollno = a; }
void put()
{ cout << rollno; }
};
member function with default args
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get(int a=5)
{ rollno = a; }
void put()
{ cout << rollno; }
};
int main()
{
Student s1;
s1.get(20);
s1.put();
s1.get();
s1.put();
return 0;
}
Multiple objects
#include <iostream>
using namespace std;
class Student
{
private:
int rollno;
public:
void get(int a)
{ rollno = a; }
void put()
{ cout << rollno; }
};
int main()
{
Student s1,s2,s3;
s1.get(10);
s2.get(20);
s3.get(30);
s1.put();
s2.put();
s3.put();
return 0;
}

More Related Content

PPT
Oops lecture 1
PPTX
Function class in c++
PPTX
Lecture-11 Friend Functions and inline functions.pptx
PDF
C++ Interview Questions and Answers PDF By ScholarHat
PPTX
Oop concept in c++ by MUhammed Thanveer Melayi
PPTX
OOC MODULE1.pptx
PPT
C++ tutorials
PPTX
Introduction to Fundamental of Class.pptx
Oops lecture 1
Function class in c++
Lecture-11 Friend Functions and inline functions.pptx
C++ Interview Questions and Answers PDF By ScholarHat
Oop concept in c++ by MUhammed Thanveer Melayi
OOC MODULE1.pptx
C++ tutorials
Introduction to Fundamental of Class.pptx

Similar to C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro (20)

PPTX
UNIT3 on object oriented programming.pptx
PPSX
Object oriented programming 2
PPTX
C++ & Data Structure - Unit - first.pptx
PPTX
PPTX
C++ tutorial assignment - 23MTS5730.pptx
PPTX
Object Oriented Programming using C++ Unit 1
PPTX
C concepts and programming examples for beginners
PPT
Unit vi(dsc++)
PPTX
Presentation on topic of c and c++ programming language.(.pptx
PPT
DS Unit 6.ppt
PPTX
Functions in C.pptx
PPTX
Functions and Modules.pptx
PDF
Unit iii
PPTX
Object oriented programming in C++
PDF
Object Oriented Programming Constructors & Destructors
PPT
Lecture02
PPTX
object oriented programming language.pptx
PPT
cs8251 unit 1 ppt
UNIT3 on object oriented programming.pptx
Object oriented programming 2
C++ & Data Structure - Unit - first.pptx
C++ tutorial assignment - 23MTS5730.pptx
Object Oriented Programming using C++ Unit 1
C concepts and programming examples for beginners
Unit vi(dsc++)
Presentation on topic of c and c++ programming language.(.pptx
DS Unit 6.ppt
Functions in C.pptx
Functions and Modules.pptx
Unit iii
Object oriented programming in C++
Object Oriented Programming Constructors & Destructors
Lecture02
object oriented programming language.pptx
cs8251 unit 1 ppt
Ad

Recently uploaded (20)

PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
master seminar digital applications in india
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Supply Chain Operations Speaking Notes -ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
Microbial diseases, their pathogenesis and prophylaxis
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Week 4 Term 3 Study Techniques revisited.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
master seminar digital applications in india
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Ad

C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro

  • 1. Module 5 Overview of Object Oriented Prgg. Features of OOP - Classes and Objects - “this” pointer - Constructors and Destructors - Static Data Members, Static Member Functions and Objects - Inline Functions – Call by reference - Functions with default Arguments - Functions with Objects as Arguments - Friend Functions and Friend Classes.
  • 2. #include <stdio.h> int main() { int num1, num2, sum; scanf("%d", &num1); scanf("%d", &num2); sum = num1 + num2; printf("Sum:%dn", sum); return 0; } #include <iostream> using namespace std; int main() { int num1, num2, sum; cin>>num1; cin>>num2; sum = num1 + num2; cout<<"Sum: "<<sum; return 0; } C C++
  • 3. #include <stdio.h> int main() { int num1, num2, sum; scanf("%d", &num1); scanf("%d", &num2); sum = num1 + num2; printf("Sum:%dn", sum); return 0; } #include <iostream> using namespace std; int main() { int num1, num2, sum; cin>>num1; cin>>num2; sum = num1 + num2; cout<<"Sum: "<<sum; return 0; } C++ compiler can execute both programs
  • 4. The key differences between old-style and modern code involve two features: new-style headers and the namespace statement.
  • 5. #include <iostream> int main() { int num1, num2, sum; std::cin >> num1; std::cin >> num2; sum = num1 + num2; std::cout << "Sum: " << sum; return 0; } #include <iostream> using namespace std; int main() { int num1, num2, sum; cin>>num1; cin>>num2; sum = num1 + num2; cout<<"Sum: "<<sum; return 0; } With namespace Without namespace
  • 6. namespace • a namespace is a collection of related names or identifiers (functions, class, variables) which helps to separate these identifiers from similar identifiers in other namespaces or the global namespace. • Namespaces are a recent addition to C++. • A namespace creates a declarative region in which various program elements can be placed. • The using statement informs the compiler that you want to use the std namespace. • This is the namespace in which the entire Standard C++ library is declared. • By using the std namespace you simplify access to the standard library
  • 7. • Standard C++ created a new kind of header that is used by the Standard C++ library. • The new-style headers do not specify filenames. • Instead, they simply specify standard identifiers that may be mapped to files by the compiler, although they need not be. • The new-style C++ headers are an abstraction that simply guarantee that the appropriate prototypes and definitions required by the C++ library have been declared. • Since the new-style headers are not filenames, they do not have a .h extension. • They consist solely of the header name contained between angle brackets. • The C++ versions of the C standard headers simply add a "c" prefix to the filename and drop the .h. For example, the C++ new-style header for math.h is cmath. The one for string.h is cstring.
  • 8. Defining a class Accessing class members from outside function Object creation: Class_name Object_name; Object_name.function_name(); Object_name.variable_name;
  • 9. Defining a class class members can be directly accessed by its member function function_name(); variable_name;
  • 10. #include <iostream> using namespace std; class Student { private: int rollno; float percentage; public: // Function to get input void get() { cout << "Enter details: "; cin >> rollno; cin >> percentage; } C++ prg with Class and object // Function to display output void put() { cout << "nStudent Details:n"; cout << rollno << “n”<<percentage; } }; int main() { Student s1; // Create an object of Student class s1.get(); // Call get() to take input s1.put(); // Call put() to display output return 0; }
  • 11. • 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. • 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. Access specifier
  • 12. • You may change access specifications as often as you like within a class declaration. Access specifier
  • 13. #include <iostream> using namespace std; class Student { private: int rollno; float percentage; public: // Function to get input void get() { cout << "Enter details: "; cin >> rollno; cin >> percentage; } // Function to display output void put() { cout << "nStudent Details:n"; cout << rollno << “n”<<percentage; } }; int main() { Student s1; s1.get(); s1.put(); s1.rollno=22; return 0; }
  • 14. #include <iostream> using namespace std; class Student { private: int rollno; void get() { cout << "Enter details: "; cin >> rollno; } void put() { cout << "nStudent Details:n"; cout << rollno; } }; int main() { Student s1; s1.get(); s1.put(); return 0; }
  • 16. Members of a class • Elements of a class. • 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.) Characteristics of member functions: • Several different classes can use the same function name. The 'membership label' will resolve their scope. • Member functions can access the private data of the class. A non- member function cannot do so. • A member function can call another member function directly, without using the dot operator.
  • 17. Defining member functions Inside the class definition Outside the class definition
  • 18. Defining member functions #include <iostream> using namespace std; class Student { private: int rollno; public: void get() { cin >> rollno; } void put() { cout << rollno; } }; #include <iostream> using namespace std; class Student { private: int rollno; public: void get(); void put(); }; void Student :: get() { cin >> rollno; } void Student :: put() { cout << rollno; }
  • 19. Nesting of member functions #include <iostream> using namespace std; class Student { private: int rollno; public: void get() { cin >> rollno; put(); } void put() { cout << rollno; } }; int main() { Student s1; s1.get(); return 0; }
  • 20. Member function with args Member function with default args
  • 21. member function with args #include <iostream> using namespace std; class Student { private: int rollno; public: void get(int a) { rollno = a; } void put() { cout << rollno; } }; int main() { Student s1; s1.get(20); s1.put(); return 0; }
  • 25. Function with default args • C++ allows us to call a function without specifying all its arguments. • In such cases, function assigns a default value to the parameter which does not have a matching argument in the function call. • Default values are specified when the function is declared. • The compiler looks at the prototype to see how many arguments a function uses and alerts the program for possible default values.
  • 26. • A default argument is checked for type at the time of declaration and evaluated at the time of call. • One important point to note is that only the trailing arguments can have default values and therefore we must add defaults from right to left. • We cannot provide a default value to a particular argument in the middle of an argument list. • Some examples of function declaration with default values are: • int mul(int i, int j=5, int k=10); • int mul(int i=5, int j); • int mul(int i=0, int j, int k=10); • int mul(int i=2, int j=5, int k=10); • // legal • // illegal • // illegal • // legal Function with default args
  • 27. • Default arguments are useful in situations where some arguments always have the same value. For instance, bank interest may remain the same for all customers for a particular period of deposit. It also provides a greater flexibility to the programmers. A function can be written with more parameters than are required for its most common application. Using default arguments, a programmer can use only those arguments that are meaningful to a particular situation. Function with default args
  • 34. member function with default args #include <iostream> using namespace std; class Student { private: int rollno; public: void get(int a=5) { rollno = a; } void put() { cout << rollno; } };
  • 35. member function with default args #include <iostream> using namespace std; class Student { private: int rollno; public: void get(int a=5) { rollno = a; } void put() { cout << rollno; } }; int main() { Student s1; s1.get(20); s1.put(); s1.get(); s1.put(); return 0; }
  • 36. Multiple objects #include <iostream> using namespace std; class Student { private: int rollno; public: void get(int a) { rollno = a; } void put() { cout << rollno; } }; int main() { Student s1,s2,s3; s1.get(10); s2.get(20); s3.get(30); s1.put(); s2.put(); s3.put(); return 0; }