Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
7 class objects
1. Class and Objects
09/04/131 VIT - SCSE
By
G.SasiKumar., M.Tech., (Ph.D).,
Assistant Professor
School of Computing Science and Engineering
VIT University
2. Class
A class is a mechanism for creating user-defined data types.
09/04/132 VIT - SCSE
class class_name
{
private data and functions
access-specifier:
data and functions
access-specifier:
data and functions
.
.
.
access-specifier:
data and functions
}object-list;
3. Access Specifier
09/04/133 VIT - SCSE
Access Specifier is one of these three C++ keywords:
1.private
2.public
3.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.
4. 09/04/134 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;
}
};
5. Class Objects
Accessing a Class Members
09/04/135 VIT - SCSE
objectname . datamember;
objectname . functionname(actual arguments);
Example:
S1.setdata(10,”Rajkumar”);
S1.outdata();
6. Class Members
09/04/136 VIT - SCSE
1. Data Member
2. Member Function
Defining a member function of a class inside its scope
Defining a member function of a class outside its scope
Syntax:
returntype classname::memberfunctions(argument1,2,..n)
{
}
7. Array of class object
09/04/137 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();
}
}
8. Pointers and classes
09/04/138 VIT - SCSE
class sample
{
int x;
float y;
public:
void getdata();
void display()
};
sample *p;
(*objectname).membername;
or
objectname->membername;
9. 09/04/139 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();
}
10. Nested Class (classes within classes)
09/04/1310 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:
//member
public:
//member
class inner{
private:
//member
public:
//member
};
};
11. 09/04/1311 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;
}
12. 09/04/1312 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();
}
13. Constructor
09/04/1313 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.
14. 09/04/1314 VIT - SCSE
Syntax:
class test
{
private:
--------------
--------------
protected:
--------------
--------------
public:
test(); //constructor
--------------
};
test::test()
{
--------------
----------------
}
15. 09/04/1315 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();
}
17. Default Constructor
09/04/1317 VIT - SCSE
A default constructor function initializes the data
members with no arguments.
Example:
class Exforsys
{
private:
int a,b;
public:
Exforsys();
...
};
Exforsys :: Exforsys()
{
a=0;
b=0;
}
18. Parameterized Constructor
09/04/1318 VIT - SCSE
Constructor with arguments
Example:
#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;
}
19. Copy Constructor
09/04/1319 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)
{
----------
}
20. 09/04/1320 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();
}
21. Destructor
09/04/1321 VIT - SCSE
A destructor is a function that automatically executes when
an object is destroyed.
class username
{
private:
//members;
public:
username(); //constructor
~username(); //destructor
};
22. 09/04/1322 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();
}
25. Static Class Members
09/04/1325 VIT - SCSE
static data member
static 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.
26. 09/04/1326 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();
}
27. Static member function
09/04/1327 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;
28. 09/04/1328 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();
}