0% found this document useful (0 votes)
18 views13 pages

Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering

The document describes a C++ program that defines a class called 'item' with private data member 'keep_data' and public methods 'set' and 'get_value' to set and retrieve the data. It instantiates three 'item' objects and assigns values to them using the 'set' method. It then prints the values retrieved using 'get_value'. It also defines a 'rectangle' class with private data members 'height' and 'width' and public methods 'area' and 'initialize' to calculate area and initialize the object. It compares the class to a normal C structure and demonstrates accessing class members only through methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Mr. Fourcan Karim Mazumder Faculty Dept. of Computer Science and Engineering

The document describes a C++ program that defines a class called 'item' with private data member 'keep_data' and public methods 'set' and 'get_value' to set and retrieve the data. It instantiates three 'item' objects and assigns values to them using the 'set' method. It then prints the values retrieved using 'get_value'. It also defines a 'rectangle' class with private data members 'height' and 'width' and public methods 'area' and 'initialize' to calculate area and initialize the object. It compares the class to a normal C structure and demonstrates accessing class members only through methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Mr.

Fourcan Karim Mazumder


Faculty
Dept. of Computer Science and
Engineering
#include <iostream.h>
// the class declaration part
class item
{
int keep_data; // private by default, it is
public in struct
public: // public part
void set(int enter_value);
int get_value(void);
}; 
// class implementation part
void item::set(int enter_value)
{
keep_data = enter_value;
}
int item::get_value(void)
{
return keep_data;
}
// main program
void main()
{
item John_cat, Joe_cat, Big_cat;
// three objects instantiated
int garfield; // a normal variable
John_cat.set(10); // assigning values 
 
Joe_cat.set(11);
Big_cat.set(12);
garfield = 13;
cout<<"Accessing data using class\n";
cout<<"-------------------------\n";
cout<<"Data value for John_cat is
"<<John_cat.get_value()<<"\n";
cout<<"Data value for Joe_cat is
"<<Joe_cat.get_value()<<"\n";
cout<<"Data value for Big_cat is
"<<Big_cat.get_value()<<"\n";
cout<<"\nAccessing data normally\n";
cout<<"---------------------------\n";
cout<<"Data value for garfield is
"<<garfield<<"\n"; 
}
Output:
Accessing data using class
-------------------------
Data value for John_cat is 10
Data value for Joe_cat is 11
Data value for Big_cat is 12
Accessing data normally
---------------------------
Data value for garfield is 13
 
// program classobj.cpp - using class instead of
struct
#include <iostream.h>
// a simple class declaration part
class rectangle
{
// private by default, member variables
int height;
int width;
 
public:
// public, with two methods
int area(void);
void initialize(int, int);
};
// class implementation part
int rectangle::area(void)
{
return (height * width);
}
void rectangle::initialize(int initial_height, int initial_width)
{
height = initial_height;
width = initial_width;
}

// a normal structure - compare it with class


struct pole
{
int length; // public
int depth; // public
};
// main program
void main ( )
{
rectangle wall, square;
pole lamp_pole;
// wall.height = 12;
// wall.width = 10;
// square.height = square.width = 8;
// these 3 lines invalid now, private, access only through
methods
wall.initialize(12,10); // access data through method
square.initialize(8,8);
lamp_pole.length = 50; // a normal struct data access
lamp_pole.depth = 6;
cout<<"Using class instead of struct\n";
cout<<"access through method area()\n";
cout<<"------------------------------\n";
cout<<"Area of the wall-->wall.area() =
"<<wall.area()<< "\n\n";
cout<<"Area of the square-->square.area()=
"<<square.area()<<"\n\n";
cout<<lamp_pole.length<<"\n";
cout<<lamp_pole.depth;
}
Output:
 
Using class instead of struct
access through method area()
------------------------------
Area of the wall-->wall.area() =120
Area of the square-->square.area()= 64
50
6
 

You might also like