C++ programming language part: 48 Classes and Objects
Classes and
Objects
The important elements of a object-oriented language are
classes. You can define many objects and functions of the same classes. All the
objects share the same copy of the member function but maintains a separate
copy of the member data.The stracture of a class as follows:
Class specifier
The specifier for a class starts with the keyword class
and Name , the body of the
class delimites by braces and is terminated by a semicolon,
class
nova
{
// member data
// member function
};
Defining Object
An object suppose K is an instant of a class called nova
as follows
nova k; // k is called object
Access Specifiers
The body of the class contains two keyword: private
and public
Private data or function can only be accesses from within the
class. Public data or functions, on the other hand, are accessible outside the
class. It is not possible to access the member data directly from function main()
as they are private to the nova class. These data can be
accessed only from the member function of that class.
//class & object.demo
#include
#include
class
nova //class defined
{
private:
int age; // member data
char name[10];
public:
void
data_in() // member function
{
cout<<"
Type your age "<
cin>>age;
cout<<"Type
your name "<
cin>>name;
}
void data_out() // member function
{
cout<<"
The age is "<
cout<<"
The name is "<
}
};
void main()
{
nova k; // k is called object
k.data_in(); //would be k->data_in() if pointer
k.data_out();
}
Program 38_2
class
nova //class defined
{ private:
int a,b,c; // member data
public:
void
data_in() // member function
{
cout<<" Type two nunbers "<
cin>>a>>b; }
void
add() // member function
{ c=a+b;
}
void
data_out() // member function
{
cout<<" The sum is "<
};
void main()
{
nova k; // k is called object
k.data_in(); //would be k->data_in() if pointer
k.add();
k.data_out();
}
জ্ঞানকোষ প্রকাশনী
৩৮/২-ক, বাংলাবাজার (২য় তলা), ঢাকা।
ফোনঃ ৭১১৮৪৪৩, ৮১১২৪৪১, ৮৬২৩২৫১.
কলকাতায় পরিবেশক/প্রাপ্তিস্থান
রিতা ইন্টারন্যাশনাল
৩৬, পি.এন. ব্যানার্জি রোড, কলকাতা
ফোনঃ ২৫১৩৮৩৫৯, ৯৮৩০৪৩৯৬৭৯, +৯১৯৮৩০৪৩৯৬৭৯