Modified Objects and Classes PL
Modified Objects and Classes PL
Specifying a Class
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
build-in data type.
Generally, a class specification has two parts:
1. Class declaration
2. Class function definitions
The class declaration describes the type scope of its members. The class function
definitions describe how the class functions are implemented.
The general form of a class declaration is:
class class_name
{
private:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
class is a keyword, class_name is the user defined name, private and public are the
access specifiers e.g
class student
private:
int roll_no;
char name[20];
int marks;
public:
void getdata(void);
void putdata(void);
};
public:
int side;
/*
*/
int getVolume();
};
If we define the function inside class then we don't not need to declare it first, we can directly
define the function.
class Cube
public:
int side;
int getVolume()
};
But if we plan to define the member function outside the class definition then we
must declare the function inside class definition and then define it outside.
class Cube
public:
int side;
int getVolume();
return side*side*side;
}
Array within a Class
The array can be used as member variables in a class. The following class definition
is valid.
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
The array variable a[] declared as private member of the class array can be used in
the member function, like any other array variable. We can perform any operations
on it. For instance, in the above class definition, the member function setval() sets
the value of element of the array a[], and display() function displays the values.
Similarly, we may use other member functions to perform any other operation on the
array values.
Example:
#include<iostream>
#include<conio.h>
class student
{
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
};
OUTPUT:
Enter roll no.: 101
Enter marks in subject 1: 55
Enter marks in subject 2: 80
Enter marks in subject 3: 73
Enter marks in subject 4: 60
Enter marks in subject 5: 84
Total marks=352
Creating Objects
The declaration of a class does not create an object but only specifies what it will
contain. For example the declaration of the class array above does not create an
object of array. Once the class has been declared we can create variables of that
type by using the class name like any other built in type variable
Hence data member of the class can contain different value for the different object,
memory allocation is performed separately for each data member for different object
at the time of creating an object.
Member function remains common for all object. Memory allocation is done only
once for member function at the time of defining it.
In the above syntax, static keyword is used. The data_type is the C++ data type such
as int, float etc. The data_member_name is the name provided to the data member.
A program that demonstrates the static data members in C++ is given as follows –
#include <iostream>
#include<string.h>
class Student {
private:
int rollNo;
char name[10];
int marks;
public:
Student() {
objectCount++;
void getdata() {
void putdata() {
cout<<endl;
}
};
int Student::objectCount = 0;
int main(void) {
Student s1;
s1.getdata();
s1.putdata();
Student s2;
s2.getdata();
s2.putdata();
Student s3;
s3.getdata();
s3.putdata();
return 0;
class Box
{
public:
static int objectCount;
// Constructor definition
Box(double l = 2.0, double b = 2.0, double h = 2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
int main(void)
{
// Print total number of objects before creating object.
cout << "Inital Stage Count: " << Box::getCount() << endl;
return 0;
}
OUTPUT:
Inital Stage Count: 0
Constructor called.
Constructor called.
Final Stage Count: 2
Array of Objects
Like array of other user-defined data types, an array of type class can also be
created. The array of type class contains the objects of the class as its individual
elements. Thus, an array of a class type is also known as an array of objects. An
array of objects is declared in the same way as an array of any built-in data type.
The syntax for declaring an array of objects is
class_name array_name [size] ;
Example : A program to demonstrate the concept of array of objects
#include<iostream>
using namespace std;
class books
{
char title[30];
float price;
public :
void getdata();
void putdata();
};
void books :: getdata ()
{
cout<<"Title:”;
Cin>>title;
cout<<"Price:”;
cin>>price;
}
void books :: putdata ()
{
cout<<"Title:"<<title<< "\n";
cout<<"Price:"<<price<< "\n”;
const int size=3 ;
}
int main ()
{
books book[size] ;
for(int i=0;i<size;i++)
{
cout<<"Enter details o£ book "<<(i+1)<<"\n";
book[i].getdata();
}
for(int i=0;i<size;i++)
{
cout<<"\nBook "<<(i+l)<<"\n";
book[i].putdata() ;
}
return 0;
}
The output of the program is
// Function declaration
void func(void);
main() {
while(count--) {
func();
}
return 0;
}
// Function definition
void func( void ) {
static int i = 5; // local static variable
i++;
std::cout << "i is " << i ;
std::cout << " and count is " << count << std::endl;
}
When the above code is compiled and executed, it produces the following result −
i is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0
main() {
count = 5;
write_extern();
}
void write_extern(void) {
std::cout << "Count is " << count << std::endl;
}
Here, extern keyword is being used to declare count in another file. Now compile
these two files as follows −
$g++ main.cpp support.cpp -o write
This will produce write executable program, try to execute write and check the
result as follows −
$./write
5
class A
public:
mutable int x;
int y;
};
int main()
const A var2;
var2.x = 345;
// var2.y = 2345;
In the above example the compiler would not allow the assignment var2.y = 2345
because var2 has been declared as const. The compiler will allow the assignment
var2.x = 345 because A::x has been declared as mutable .
Distinction between structures in C and Class
C structures cannot have member function while C++ class always has.
C structures must have at least one data member in it to compile. C++ class can be
empty. It is possible to have a C++ class without member variables and member
functions.
Static member variables are not allowed in C structures. C++ can have static
members.