Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Introduction to class and objects:

Class:
A class is a data type defined by the user describing the data it represents and the
function to be used to manipulate the data. It seems as a plan/template/blue
print.

Object:
Objects are instances of class, which holds the data variables declared in class and
the member functions work on these class objects. We can define many objects of
the same class. Class and their objects can be defined according to the problem at
hand. e.g. Animal is a class and cat, dog, lion, tiger objects of class animal.

Creating classes:
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.
Creating Classes (Another answer; for Long Question)
A class is a way to bind the data and its associated functions together. It allows the data (and
functions) to be hidden, if necessary, from external use. When defining a class, we are creating
a new abstract data type that can be treated like any other built-in data type. The general
form of a class declaration is:

class class_name
{
private:
variable declaration;
function declarations;
protected:
variable declaration;
function declarations;
public:
variable declarations;
function declaration;
};

The class declaration is similar to a struct declaration. The keyword class specifies, that follows
is an abstract data of type class_name. The body of a class is enclosed within braces and
terminated by a semicolon. The class body contains the declaration of variables and functions.
These functions and variables are collectively called class members.
The keywords private,protected and public are known as visibility labels or access specifier.
Example of Creating class:
class student
{
int roll_number;
float marks;
public:
void insertdata(int a, float b);
void displaydata(void);
};

Access Specifiers (Public, Private, Protected)


Access specifiers are keywords in object oriented programming languages
which describe the behaviour or accessibility of classes, methods and
members. Its members access is governed by three access specifiers
namely; Private, public and protected.

By default, access to members of a C++ class is private. This means that


members of the class are not accessible outside the class but can be
accessed via the methods of the class.

A public access specifier means that its members are available


everywhere in the program. This is because they are often not too
sensitive.
A protected access specifier is similar to a private access specifier but
then it can be accessed by child classes of the current class.

(Public, Private, Protected)


Advanced college ko Notema ramro answer cha

Defining Member Function in C++


A member function of a class is a function that has its definition or its prototype within the
class definition like any other variable. It operates on any object of the class of which it is a
member, and has access to all the members of a class for that object. If the member function
is defined inside the class definition it can be defined directly, but if its defined outside the
class, then we have to use the scope resolution :: operator along with class name along with
function name.

1. Inside the class


If we define the function inside class then we don't not need to declare it first, we can directly
define the function.

Example:
class Cube
{
public:
int side;
int getVolume()
{
return side*side*side; //returns volume of cube
}
};

2. Outside the class


But to define the member function outside the class definition then we must
declare the function inside class definition and then define it outside.
To define a function outside of a class, scope resolution operator :: is used.
Syntax:
return_type class_name::function_name(argument declaration)
{
Function body
}

Example:
class Cube
{
public:
int side;
int getVolume();
}

// member function defined outside class definition


int Cube :: getVolume()
{
return side*side*side;
}

Characteristics of Member functions: (Mam slide bata copied)


 Different classes can use same function name. ‘Membership label’ resolves
their scope
 Member functions can access private data of the class. A non-member
function can’t do so (Exception: friend function)
 A member function can call another member function directly, without using
the dot operator.

Characteristics of member functions: (Internet search)

1) A program can have several classes and they can have member functions with the same
name, Ambiguity is resolved using the scope resolution operator (::)
2) Private members of a class can be accessed by all the members of the class, whereas non-
member functions are not allowed to access.
3) Member functions of the same class can access all other members of their own class
without the use of dot operator.
4) Member functions defined as public act as an interface between the service provider and
the service seeker.
5) Member functions can take default arguments as well.

Nesting of Member Functions


A member function can call another member function of the same class directly without
using the dot operator. This is called as nesting of member functions.
Only the public members of a class can be accessed by the object of that class, using dot
operator. However a member function can call another member function of the same class
directly without using the dot operator.
Example:
#include<iostream>
using namespace std;
class add
{
public:
void get();
void display();
};
void add ::get()
{
cout<<"Enter x and y;
cin>>x>>y;
display();
}
void add ::display()
{
cout<<"z="<<z;
}
int main()
{
add ob;
ob.get();
ob.display();
return 0;

Static Data Members:


Static data members are class members that are declared using the ‘static’
keyword.
Each object in a class has its own separate data. If all the objects need
to share one data, a slight change in the class specification would be sufficient. The
modification required would be to give a keyword “static” for the data which is to
be shared. “If a data item in a class is defined as static, then only one such item is
created for the entire class, no matter how many objects are there”. A member
variable defined as static has similar characteristics to a normal static variable. It is
visible only within the class, but its lifetime is the entire program. Some compilers
automatically initialize a static data item, where other does not.

The properties of a static member variable are similar to that of a C static variable. A static
member variable has certain special characteristics. These are:
• It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
• Only one copy of that member is created for the entire class and is shared by all the objects
of that class, no matter how many objects are created.
• It is visible only within the class, but its lifetime is the entire program.

Static variables are normally used to maintain values common to the entire class. For example,
a static data member can be used as a counter that records the occurrences of all the objects.
The type and scope of each static member variable must be defined outside the class
definition. This is necessary because the static data members are stored separately rather than
as a part of an object. Since they are associated with the class itself rather than with any class
object, they are also known as class variables. The static variable a is initialized to zero when
the objects are created.

Static Member Functions:


Like static member variable, we can also have static member functions. A static
member function is one which can be called directly form main ( ) without
associating it to an object. In other words a static member function can be called
without creating instances / objects of the class.
A member function that is declared static has the following properties:
• A static function can have access to only other static members (functions or
variables) declared in the same class.
• A static member function can be called using the class name (instead of its
objects) as follows:
classname :: functionname;

Example 1 : (Compile bhayena tara yo program)


#include<iostream>
using namespace std;
class A
{
static int a;
int b;
public:
void displaya()
{
cout<<"a="<<a<<endl;
a=a+1;
}
void displayb()
{
cout<<"b="<<b<<endl;
b=b+1;
};
int A::a;
main()
{
A obj1,obj2,obj3;
Obj1.displaya();
obj2.displaya();
obj3.displaya();
obj1.displayb();
obj2.displayb();
obj3.displayb();
}
Output
a= 0
a= 1
a= 2
b= 7756
b= 0
b= 1206

Example 2:
#include<iostream>
using namespace std;
class my
{
static int count; //declaration of one data item for all objects
public:
my ( )
{
count ++;
}
int getcount ( )
{
return count;
}
};
int my::count = 0; // definition of count which alloates memory for count, and initializes it
int main ( )
{
my f1,f2,f3;
cout<<"count is "<<f1.getcount ( )<<endl;
cout<<"count is "<<f2.getcount ( )<<endl;
cout<<"count is "<<f3.getcount ( )<<endl;
return 0;
}
Output:
count is 3
count is 3
count is 3
Friend Function
A friend function is a function that is not a member of a class but has access to the
class's private and protected members.
The concepts of data hiding and encapsulation dictates that private and protected
members of a class cannot be accessed from outside the class. This is non-member
function of a class cannot access the non-public members (data members and
member functions) of a class. However, we can achieve this by using friend
functions. The friend functions allow operations between two different classes.
Generally the use of friend functions is out of an object-oriented proframming
methodology because it violates the concept of data hiding. So whenever possible
it is better to use members of the same class. To make an outside function friendly
to a class, we simply declare the function as a friend of the class.
Some special characteristics of friend functions are:
1. Since, it is not in the scope of the class to which it has been declared as a friend,
it cannot be called using the object of the class.
2. It can be invoked like a normal function without the help of any object.
3. Unlike member functions, it cannot access the member names directly and has
to use an object name and dot membership operator with each member name.
4. It can be declared either in the public or the private part of a class without
affecting its meaning.
5. It has the objects as arguments.
Friend function also acts as bridging between two classes. For example, if
we want a function to take objects of two classes as arguments and
operate on their private members, we can inherit the two classes from
the same base class and put the function in the base class.

Example
#include<iostream>
using namespace std;
class A
{
int a;
int b;
friend void sum(A ob);
public: void setvalue(int x, int y)
{
a=x;
b=y;
}
};
void sum(A ob)
{
cout<<ob.a+ob.b;
}
main()
{
A obj;
obj.setvalue(5,3);
sum(obj);
}
Output
8

Characteristics of friend function (Mam Slide)


1. Friend function is not in the scope of the class in which it has been
declare as friend. So friend function cannot be called with the help
of object of that class.
2. Friend function can be invoked like a normal function without any
help of any object.
3. Friend function cannot access member name directly. It has to use
object name with dot operator.
4. Friend function can be declared either in public or private part of the
class without affecting its meaning.
5. Friend function can access many object of many class in which it has
been declared as friend.
6. Usually, friend function has object as argument.
End Semester Questions:
1. Differentiate between private and public access modifier. What is the
importance of having protected modifier?
2. Differentiate between abstract class and concrete class.
3. What are static member and static function? Explain with appropriate syntax.
4. Define private member function. Write a C++ program to illustrate private
member function that sorts user given 10 (ten) integers in descending order.
5. What is class and object in C++?
6. We know that a private member of a base class is not inheritable. Is it any way
possible for the objects of a derived class to access the private members of the
base class? If yes, how?
[Remember, the base class cannot be modified.]
Inheritanceko Question ho
7. Write short notes on Static data members and static member functions.
8. Using class and object, write a program to print 10 integers in ascending order.
9. Write short notes on
a) Access Specifiers
b) Private member function and static member function
c) Function overloading and function overriding

1. Differentiate between private and public access modifier. What is the


importance of having protected modifier?
Private access modifier Public access modifier
1. The class members declared as private can 1. All the class members declared under
be accessed only by the functions inside the public will be available to everyone.
class.
2. The private keyword is used to create 2. The public keyword is used to create
private members (data and functions). public members (data and functions).
3. Data members are normally declared with 3. Member functions are normally declared
the private access specifier. with the public access specifier.
4. The public members of a class cannot be 4. The public members of a class can be
accessed directly by using dot operator with accessed from anywhere in the program
the object of that class. using the direct member access operator (.)
with the object of that class.
5. The data and the member functions 5. The data and the member functions
declared inside the private access modifiers declared inside the public access modifiers
are more secure than in public. are less secure than in private.

Protected access modifier is similar to that of private access modifiers, the difference is that
the class member declared as Protected are inaccessible outside the class but they can be
accessed by any subclass (derived class) of that class.
(For more and the example look at the ACEM note)

4. Define private member function. Write a C++ program to illustrate private


member function that sorts user given 10 (ten) integers in descending order.
 Functions defined inside the class's private section are private member functions. The
public members of a class are accessible from anywhere outside the class within a program. In
order to hide the data members of the class they are made private since private members of
class cannot be accessed outside the class. Only the class itself and friend function can access
private members. By default all the members of a class would be private, which means until
you label a member, it will be assumed a private member.

6. We know that a private member of a base class is not inheritable. Is it any way
possible for the objects of a derived class to access the private members of the
base class? If yes, how?
[Remember, the base class cannot be modified.]
 We know that member of one class can not access the private data of other
class. Some time it is needed that we want to make available private data of
one class to other class. In such case we need to make one class to a friend of
other class.

A base class's private members are never accessible directly from a derived
class, but can be accessed through calls to the public and protected members
of the base class.

You might also like