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;
}
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.
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;
}