Class and Objects 06/15/22 1 VIT - SCSE By G.SasiKumar., M.Tech., (Ph.D)., Assistant Professor School of Computing Science and Engineering VIT University
May 06, 2015
Class and Objects
04/11/231 VIT - SCSE
By
G.SasiKumar., M.Tech., (Ph.D).,Assistant Professor
School of Computing Science and EngineeringVIT University
ClassA class is a mechanism for creating user-defined data types.
04/11/232 VIT - SCSE
class class_name{private data and functionsaccess-specifier:data and functionsaccess-specifier:data and functions...access-specifier:data and functions}object-list;
Access Specifier
04/11/233 VIT - SCSE
Access Specifier is one of these three C++ keywords:
1.private2.public3.protected
A private member within a class denotes that only members of the same class have accessibility. The private member is inaccessible from outside the class.
Public members are accessible from outside the class.
A protected access specifier is a stage between private and public access. If member functions defined in a class are protected, they cannot be accessed from outside the class but can be accessed from the derived class.
04/11/234 VIT - SCSE
class student{int a,b;public :void setdata(int x,int y){a=x;b=y;}void outdata(){cout<<“A =“<<a<<endl;cout<<“B= “<<b<<endl;}};
Class ObjectsAccessing a Class Members
04/11/235 VIT - SCSE
objectname . datamember;objectname . functionname(actual arguments); Example:
S1.setdata(10,”Rajkumar”);S1.outdata();
Class Members
04/11/236 VIT - SCSE
1. Data Member2. Member Function
Defining a member function of a class inside its scopeDefining a member function of a class outside its scope Syntax: returntype classname::memberfunctions(argument1,2,..n){ }
Array of class object
04/11/237 VIT - SCSE
#include<iostream.h>class student{private:long int rollno;int age;public:void getinfo(){cout<<”Roll no:”;cin>>rollno;cout<<”Age:”;cin>>age;}void disinfo(){cout<<endl;cout<<”Roll No =”<<rollno<<endl;cout<<”Age =”<<age<<endl;}};
void main(){student s[5];int i,n;cout<<”How many students?”<<endl;cin>>n;cout<<”Enter the following Information”<<endl;for(i=0;i<=n-1;i++){int j=i;cout<<endl;cout<<”record=”<<j+1<<endl;s[i].getinfo();}cout<<”Contents of class”<<endl;for(i=0;i<=n-1;i++){s[i].disinfo();}}
Pointers and classes
04/11/238 VIT - SCSE
class sample{int x;float y;public:void getdata();void display()};sample *p;
(*objectname).membername;orobjectname->membername;
04/11/239 VIT - SCSE
#include<iostream.h>class student{private:long int rollno;int age;public:void getinfo();void disinfo();};void student::getinfo(){cout<<”Roll no:”;cin>>rollno;cout<<”Age:”;cin>>age;}
void student::disinfo(){cout<<endl;cout<<”Roll No =”<<rollno<<endl;cout<<”Age =”<<age<<endl;}void main(){student *p;(*p).getinfo();p->disinfo();}
Nested Class (classes within classes)
04/11/2310 VIT - SCSE
A class declared as a member of another class is called as a nested class.The name of a nested class is local to the enclosing class.
Syntax:
class outer{private:
//memberpublic:
//memberclass inner{
private://member
public://member};
};
04/11/2311 VIT - SCSE
class sample{private:int a,b;public:int x,y;class simple{public:int x,y;void put();void display();};void get();void show();};
void sample::get(){cout<<”Enter a and b values”<<endl;cin>>a>>b;}void sample::show(){cout<<”A=”<<a<<endl;cout<<”B=”<<b<<endl;}
04/11/2312 VIT - SCSE
void sample::simple::put(){cout<<”Enter x and y values”<<endl;cin>>x>>y;}void sample::simple::display(){cout<<”X=”<<x<<endl;cout<<”Y=”<<y<<endl;}
void main(){sample s;sample::simple ob;s.get();s.show();ob.put();ob.display();getch();}
Constructor
04/11/2313 VIT - SCSE
A constructor is a special member function for automatic initialization of an object.Whenever an object is created, the special member function, i.e., the constructor will be executed automatically.
Rules for writing constructor:
1.A constructor name must be the same as that of its class name.2.It is declared with no return type (not even void).3.It cannot be declared const but a constructor can be invoked with const objects.4.It may not be static.5.It may not be virtual.6.It should have public or protected access within the class.
04/11/2314 VIT - SCSE
Syntax:
class test{private:----------------------------protected:----------------------------public:test(); //constructor--------------};test::test(){------------------------------}
04/11/2315 VIT - SCSE
// generation of the fibonacci series using constructor.#include<iostream.h>#include<conio.h>class test{unsigned long int f0,f1,fib;public:test(){f0=0;f1=1;fib=f0+f1;}void increment(){f0=f1;f1=fib;fib=f0+f1;}
void display(){cout<<fib<<‘\t’;}};void main(){test t;for(int i=0;i<=10;i++){t.display();t.increment();getch();}
Types of Constructor
04/11/2316 VIT - SCSE
1. Default Constructor2. Parameterized Constructor3. Copy Constructor
Default Constructor
04/11/2317 VIT - SCSE
A default constructor function initializes the datamembers with no arguments.
Example:class Exforsys
{ private: int a,b; public: Exforsys(); ...};
Exforsys :: Exforsys(){ a=0; b=0;}
Parameterized Constructor
04/11/2318 VIT - SCSE
Constructor with argumentsExample:#include <iostream>
using namespace std;class myclass { int a, b;public: myclass(int i, int j) { a=i; b=j; } void show() { cout << a << " " << b; }};int main(){ myclass ob(3, 5); ob.show(); return 0;}
Copy Constructor
04/11/2319 VIT - SCSE
• Copy constructors are always used when the compiler has to create a temporary object of a class object.
• The copy constructors are used in the following situations:
1. The initialization of an object by another object of the same class.
General format:classname::classname(classname &p){
----------}
04/11/2320 VIT - SCSE
class test{int a,b;public:test(){a=2;b=4;}test(test &p){a=p.a;b=p.b;}void display(){cout<<"a="<<a<<endl;cout<<"b="<<b<<endl;}};
void main(){test t1;t1.display();test t2;t2=t1;t2.display();getch();}
Destructor
04/11/2321 VIT - SCSE
A destructor is a function that automatically executes when an object is destroyed.
class username{private://members;public:username(); //constructor~username(); //destructor};
04/11/2322 VIT - SCSE
#include<iostream.h>#include<conio.h>class test{public:int a,b;test(){a=0;b=0;}
~test(){cout<<"Object Destroyed";}};void main(){test t1;test t2;test t3;getch();}
Constructor Overloading
04/11/2323 VIT - SCSE
Constructor with different arguments
test();test(int a);test(int a,int b);test(int a,int b,int c);
04/11/2324 VIT - SCSE
class sample{private:int a,b,c;public:sample(){a=0;b=0;c=0;}sample(int x){a=x;b=2;c=3;}sample(int x,int y,int z){a=x;b=y;c=z;}
void display();};void sample::display(){cout<<"A="<<a<<endl;cout<<"B="<<b<<endl;cout<<"C="<<c<<endl;}void main(){sample s1;cout<<"Contents of s1:"<<endl;s1.display();sample s2(10);cout<<"Contents of s2:"<<endl;s2.display();sample s3(4,8,12);cout<<"Contents of s3:"<<endl;s3.display();getch();}
Static Class Members
04/11/2325 VIT - SCSE
static data memberstatic member function
1.Static variables are common to all the class objects.2.Only one copy of that member is created and shared by all the objects of that class.3.It is visible only within the class, but the life time is the entire program.
04/11/2326 VIT - SCSE
class sample{private:static int a;public:void display();};int sample::a;void sample::display(){cout<<”content of the static data member=”;cout<<a;cout<<endl;}
void main(){sample s;s.display();getch();}
Static member function
04/11/2327 VIT - SCSE
1. A static member function can access only other static members declared in the same class.
2. It can be called using the class name instead of objects as follows,class_name :: function_name;
04/11/2328 VIT - SCSE
class sample{private:static int a;public:sample();static void display();};int sample::a=5;sample::sample(){++a;}
void sample::display(){cout<<”Value=”<<a<<endl;}void main(){sample::display();sample s1,s2,s3;sample::display();getch();}