Classes
If we say that a particular programming language is Object Oriented then it means, the programming language must have the facility to define classes in it. A class is an expanded concept of a data structure i.e instead of containing data only, it can contain both data and functions (operations on data).
What is a Class?
A class is an orgnisation of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object.
In non-technical language, we can say that a class is a collection of similar object containing a set of shared characteristics. For example , mango, apple and orange are members of the class fruit. In a way, a class and its objects have the relationship of a data type and variables. A class is simply a template for holding objects. A class is abstract but objects are real. Simply by defining a class we don’t create an object just like the mere declaration of a data ”
does not create variables. One or more classes grouped together constitute a program. Once a class has been defined, we can create any number of objects belonging to that class. For example , the syntax used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit. A class is declared in the beginning of a program. A class can contain any number of data members and any number of member functions. A class can also have only data members.
How to declare a class in C++ Language
Here is an example of how to declare a class in C++ and create the objects of that class.
class Line {
public:
Draw();
private:
int x1;
int x2;
int y1;
int y2;
};
Line line1,line2;
Both line1 and line2 are the objects of Line class.
Classes
A class is an orgnisation of data and functions which operate on them. Data structures are called data members and the functions are called member functions, The combination of data members and member functions constitute a data object or simply an object. In non-technical language, we can say that a class is a collection of similar object containing a set of shared characteristics. For example , mango, apple and orange are members of the class fruit In a way, a class and its objects have the relationship of a data type and variables. A class is simply a template for holding objects. A class is abstract but objects are real. Simply by defining a class we don?t create an object just like the mere declaration of a data ” does not create variables. One or more classes grouped together constitute a program. Once a class has been defined, we can create any number of objects belonging to that class. For example , the syntax used to create an object is no different than the syntax used to create an integer object in C. If fruit has been defined as a class, then the statement fruit mango; will create an object mango belonging to the class fruit. A class is declared in the beginning of a program. A class can contain any number of data members and any number of member functions. A class can also have only data members.
The first thing to do in object oriented programming is decide on a class hierarchy – that is, a hierarchy of kinds of objects, from very general ones to very specific kinds. In this simple example we can have a general class of ’shapes’ and more specific classes for ‘circles’ and ‘rectangles’ (a realistic program would of course have many more such classes). A particular circle with specific size, colour etc will be an instance of the circle class. We can now decide on which operations and datafields can be defined for the most general class (shape). The following gives the type definition, and an example method (assuming that the type “ColourType” is defined somewhere).
class Shape {
public:
Move(int x, int y);
SetColour(ColourType c);
private:
int xpos,ypos;
ColourType colour;
};
Shape::SetColour(ColourType c)
{
colou
Tags: C Programming, Class, Classes, Define Classes, Object, Programming
Like What you See?
Become one of the regulars by subscribing! You'll be the first to know when we add more great posts just like this. Join up by either RSS Feeds or Email Updates today!
There are 11 Comments to this post. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response or TrackBack from your own site.



































Here is a program that converts Binary to Decimal taken from
http://www.daniweb.com/techtalkforums/thread10227.html
#include "string.h" int btoi(char *buf) { // Create local variables int count, tmp; int retValue = 0; // Get length of string count = strlen(buf); // Go through string one byte at a time (backwards) for (i = 0; i <= count; i++) { // If a number '1' was found if (buf[count-i] == '1') { tmp = 1; for (j = 1; j < i; j++) tmp *= 2; retValue += tmp; } } // Return our decimal value return retValue;#include "stdio.h" short decimal2binary(unsigned long decimal_value, char binary_value[32]) { short index,significant_digits=0; unsigned long temp_value; for(index=31;index>=0;index--) { // temp_value=decimal_value/pow(2,index) temp_value=decimal_value/(1<<index); if(temp_value>0) { binary_value[index]=(char)('0'+temp_value); // decimal_value=decimal_value%pow(2,index) decimal_value=decimal_value%(1<<index); if(!significant_digits) significant_digits=index; } else { binary_value[index]='0'; } } return significant_digits; } short decimal2hex(unsigned long decimal_value, char hex_value[8]) { short index,significant_digits=0; unsigned long temp_value; for(index=7;index>=0;index--) { // temp_value=decimal_value/pow(16,index) temp_value=decimal_value/(1<<(index<<2)); if(temp_value>9) { hex_value[index]=(char)('A'-10+temp_value); // decimal_value=decimal_value%pow(16,index) decimal_value=decimal_value%(1<<(index<<2)); if(!significant_digits) significant_digits=index; } else if(temp_value>0) { hex_value[index]=(char)('0'+temp_value); // decimal_value=decimal_value%pow(16,index) decimal_value=decimal_value%(1<<(index<<2)); if(!significant_digits) significant_digits=index; } else { hex_value[index]='0'; } } return significant_digits; } void main() { short significant_digits,index; char hex_value[8]; char binary_value[32]; /* * Do the hex conversion first */ significant_digits=decimal2hex(0x0123FEDC,hex_value); printf("0x0123FEDC = 0x"); /* * If you want leading zeros use * for(index=8;index>=0;index--) */ for(index=significant_digits;index>=0;index--) printf("%c",hex_value[index]); /* * Do the binary conversion next */ significant_digits=decimal2binary(0x0123FEDC,binary_value); printf(" = 0b"); /* * If you want leading zeros use * for(index=31;index>=0;index--) */ for(index=significant_digits;index>=0;index--) printf("%c",binary_value[index]); printf(" "); }Sir i want to learn classes from the beginning
R/Sir
Please help to us use a class in function and provide some example if possible.
thanks
MN Sharma
Tell me about this pointer in C++
sir, can you give a sample program that is all about classes.
thank you in and god bless.
happy day@ you!!!!!!!
Can you give me a program that involve in class, to evaluate the function Elevator and its component, if the user ask to input 5, it display “Going up – now at floor “, then display the current floor, then if the user input 1, it display “Going down – now at floor “, then display again the current floor, and if wrong input done by the user, it display “Do nothing”,
Hope you could give me a sample program of this.
thank you in advance…
henry
Please give me a sample program of classes implementation in C++. thank’s
sir, can you give a sample program for modifying data in a file.
I need help creating a program that solves a quadratic equation using classes.
what are the static storage classes
and
Runtime structure: object