Unit 2
Unit 2
#include <iostream.h>
class Great
{ Output:
public: // Access specifier
string name; // Data Members The name you want to enter: Pratibha
void printname() // Member Functions()
{
cout << “The name you want to enter: " ;
cin>>name;
}
};
int main() {
Geeks obj1; // Declare an object of class Great
obj1.printname(); // accessing member function
return 0;
}
Static Data Members
• We can define class members static using static keyword.
• When we declare a member of a class as static it means no matter how many objects of the class
are created, there is only one copy of the static member.
• All static data is initialized to zero when the first object is created, if no other initialization is
present.
• We can't put it in the class definition but it can be initialized outside the class as done in the
following example by redeclaring the static variable, using the scope resolution operator :: to
identify which class it belongs to.
#include<iostream> return 0;
using namespace std; }
class student };
{ int student::rollno=0;
static int rollno;
char name[20]; int main()
public: {
int input() student s1,s2;
{ s1.input();
//cin>>rollno>>name; s1.output();
cin>>name; s2.input();
rollno++; s2.output();
return 0; s1.output();
} s2.output();
int output() return 0;
{ }
cout<<rollno<<name;
Use of static data members
● Static data members are sharable to all objects.
● Static member functions can access only static data members.
● Static data members to be declared outside the class.
● Static data functions can be accessed using classname and without object
with the help of scope resolution operator.
Static Function Members
● By declaring a function member as static, you make it independent of any particular
object of the class.
● A static member function can be called even if no objects of the class exist and the
static functions are accessed using only the class name and the scope resolution
operator (::)
● A static member function can only access static data member, other static member
functions and other member functions from outside the class.
#include<iostream> static int objectcount(){
using namespace std; cout<<"\nstudent count="<<scount;
class student
{ }
int rollno; };
char name[20]; int student::scount=0;
static int scount; int main()
public: {
int input() student s1,s2;
{ s1.input();
cin>>rollno>>name; s1.output();
scount++; student::objectcount();
return 0; s2.input();
} s2.output();
int output() student::objectcount();
{ s1.objectcount();
cout<<rollno<<name; //s1.output();
return 0; //s2.output();
} return 0;
}
Constructors
● A constructor is a member function of a class which initializes objects of a class.
● In C++, Constructor is automatically called when object(instance of class) create.
● It is special member function of the class (as it has no return type and has the same
name as the class name)
};
Initializer List in C++
● Initializer List is used in initializing the data members of a class.
● The list of members to be initialized is indicated with constructor as a comma-separated
list followed by a colon.
● Works as default as well as parameterized constructor
int output()
{
cout<<rollno<<marks;
return 0;
}
};
Const member functions
● Like member functions and member function arguments, the objects of a class can also be
declared as const.
● An object declared as const cannot be modified and hence, can invoke only const member
functions as these functions ensure not to modify the object.
● A const object can be created by prefixing the const keyword to the object declaration.
● Any attempt to change the data member of const objects results in a compile-time error.
Syntax:
const Class_Name Object_name;
Note:
*const object can call only const functions
*const objects can be initialized with initializer list
Sample program const
#include<iostream> int main()
using namespace std; {
class student const student s1;
{ s1.output();
int rollno; const student s2(100, 105);
int marks; s2.output();
public: Student s3;
student(int r=0, int m=100):rollno(r),marks(m){} return 0;
int output() const }
{
cout<<rollno<<marks;
return 0;
}
};
Inline Functions
● C++ provides an inline functions to reduce the function call overhead.
● Inline function is a function that is expanded in line when it is called.
● When the inline function is called whole code of the inline function gets inserted or
substituted at the point of inline function call.
● Substitution is performed by compiler at compile time
● An inline function may increase efficiency if it is small
● Syntax:
inline return-type function-name(parameters){
// function code
}
#include<iostream>
cin>>rollno>>name;
using namespace std;
return 0;
class student
}
{
int rollno;
int main()
char name[20];
{
public:
student s1;
int input();
s1.input();
int output()
s1.output();
{
return 0;
cout<<"\nthe roll no and name is";
}
cout<<rollno<<name;
return 0;
}
};
inline int student::input()
{
cout<<"\nenter the roll no. and name";
inline int student::input()
Array of objects {
cout<<"\nenter the roll no. and name";
cin>>rollno>>name;
#include<iostream> return 0;
using namespace std; }
class student
{ int main()
int rollno; {
char name[20]; student s[2];
public: for(int i=0;i<2;i++)
int input(); s[i].input();
int output() cout<<endl;
{ for(int i=0;i<2;i++)
cout<<"\nthe roll s[i].output();
no and name is"; return 0;
}
cout<<rollno<<name;
return 0;
}
};
Friend function in C++
● A friend function can be given special grant to access private and protected members of
a class.
● A friend function of a class is defined outside that class' scope but it has the right to
access all private and protected members of the class.
● Even though the prototypes for friend functions appear in the class definition, friends
are not member functions.
/* C++ program to demonstrate the working of
// friend function definition outside the class
friend function.*/
int output(student s)
{
#include <iostream>
//accessing private data from non-member function
using namespace std;
cout<<s.rollno;
return 0;
class student
}
{
int rollno;
int main()
public:
{
int input()
student s1;
{
s1.input();
cout<<"enter the rollno";
output(s1);
cin>>rollno;
return 0;
return 0;
}
}
//friend function declaration inside the class
friend int output(student s);
};
Pointer to Object
● A pointer to a C++ class is done exactly the same way as a pointer to a structure and to access
members of a pointer to a class
● You use the member access operator -> operator, just as you do with pointers to structures.
● Also as with all pointers, you must initialize the pointer before using it.
#include<iostream> int main()
using namespace std; {
student s1,s2;
class student s1.input();
{ s2.input();
int rollno; student *s;
char name[20]; s=&s1;
public: s->output();
int input() s=&s2;
{ s->output();
cout<<"enter the rollno and name"; return 0;
cin>>rollno>>name; }
return 0;
}
int output()
{
cout<<rollno<<name;
return 0;
}
};
This Pointer
● Every object in C++ has access to its own address through an important pointer called
this pointer.
● The this pointer is an implicit parameter to all member functions. Therefore, inside a
member function, this may be used to refer to the invoking object.