I like to learn new technologies. These posts are for my own reference (I have habit of forgetting). If anybody finds these posts useful, then it will surely make me HaPpY.
Showing posts with label class. Show all posts
Showing posts with label class. Show all posts
Thursday, January 10, 2013
Saturday, December 15, 2012
Pointer with classes
Pointer with classes:
c++ supports dynamic binding which is one of the powerful features of object oriented programming. To achieve dynamic binding we require virtual functions, which is implemented by concept of pointers with classes and object. Here we discuss concept of pointer, after that we explain virtual function.
1.1 Pointer of objects:
Car * santro; (is a pointer to an object of class Car)
as it happened with datastructure, in order to refer directly to a member of an object pointed by a pointer we use the arrow operator (->) of indirection. here is an example with some possible combinations:
#include <iostream>
using namespace std;
class CRectangle
{
int width, height;
public:
void set_values (int, int);
int area(void)
{
return (width * height);
}
};
void CRectangle::set_values (int a, int b)
{
width = a;
height = b;
}
int main()
{
CRectangle a, *b, *c;
CRectangle *d = new CRectangle[2];
b = new CRectangle;
c = &a;
a.set_values (1, 2);
b->set_values (3, 4);
d->set_values (5, 6);
d[1].set_values (7, 8);
cout << "\n a area: " << a.area();
cout << "\n *b area: " << b->area();
cout << "\n *c area: " << c->area();
cout << "\n d[0] area: " << d[0].area();
cout << "\n d[1] area: " << d[1].area();
}
Wednesday, November 28, 2012
How classes are implement in real software
Classes are often constructed with two files, and used with a third
1) Header (.h)
Code file that contains the class template
2) Implementation (.cpp)
Code file that contains the class implementation
Defines the actions of the methods, etc…
3) Driver (.cpp)
Code file that uses these two files to create an instance of the object
___________________________________________________________________
// 1) Header (.h)
//Filename:name.h
class name
{
private:
char *_firstName;
char *_lastName;
public:
char *getFirstName();
char *getLastName();
void setFirstName(char*);
void setLastName(char*);
};
____________________________________________________________________________________
// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"
void name :: setFirstName(char *firstName)
{
_firstName = firstName;
}
void name :: setLastName(char *lastName)
{
_lastName = lastName;
}
char* name :: getFirstName()
{
return _firstName;
}
char* name :: getLastName()
{
return _lastName;
}
___________________________________________________________________________________
// 3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;
int main()
{
name myName;
myName.setFirstName("Honey");
myName.setLastName("singh");
cout << "Hello "
<< myName.getFirstName() << " "
<< myName.getLastName() << endl;
return 0;
}
___________________________________________________________________
OUTPUT:
Hello Honey singh
1) Header (.h)
Code file that contains the class template
2) Implementation (.cpp)
Code file that contains the class implementation
Defines the actions of the methods, etc…
3) Driver (.cpp)
Code file that uses these two files to create an instance of the object
___________________________________________________________________
// 1) Header (.h)
//Filename:name.h
class name
{
private:
char *_firstName;
char *_lastName;
public:
char *getFirstName();
char *getLastName();
void setFirstName(char*);
void setLastName(char*);
};
____________________________________________________________________________________
// 2) Implementation (.cpp)
//Filename: name.cpp
#include "name.h"
void name :: setFirstName(char *firstName)
{
_firstName = firstName;
}
void name :: setLastName(char *lastName)
{
_lastName = lastName;
}
char* name :: getFirstName()
{
return _firstName;
}
char* name :: getLastName()
{
return _lastName;
}
___________________________________________________________________________________
// 3) Driver (.cpp)
//Filename: NameDriver.cpp
#include <iostream>
#include "name.cpp"
using namespace std;
int main()
{
name myName;
myName.setFirstName("Honey");
myName.setLastName("singh");
cout << "Hello "
<< myName.getFirstName() << " "
<< myName.getLastName() << endl;
return 0;
}
___________________________________________________________________
OUTPUT:
Hello Honey singh
Wednesday, October 31, 2012
What is Template
Template helps us to create the generic functions and classes. In the generic function or class, the type of data upon which the function or class operates and specified as a parameters.
//Function Template
#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b)
{
T result;
result = (a>b)?a:b ;
return (result);
}
int main()
{
int i = 5, j = 6, k;
long l = 10, m = 5, n;
//Function Template
#include <iostream>
using namespace std;
template <class T>
T GetMax (T a, T b)
{
T result;
result = (a>b)?a:b ;
return (result);
}
int main()
{
int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = GetMax<int>(i,j);
n = GetMax<long>(l,m);
cout << k << endl;
cout << n << endl;
}
OUTOUT:
6
10
int x, y;
GetMax <int> (x,y);
when the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appereance of mytype by the type passed as the actual template parameter (int in this case) and then calls it. this process is automatically performed by the compiler and is invisible to the programmer.
when the compiler encounters this call to a template function, it uses the template to automatically generate a function replacing each appereance of mytype by the type passed as the actual template parameter (int in this case) and then calls it. this process is automatically performed by the compiler and is invisible to the programmer.
______________________________________________________________________
// function Template 2
#include <iostream>
using namespace std;
template <class T>
T GetMax(T a, T b)
{
T result;
result = (a>b)?a:b ;
return (result);
}
int main()
{
int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i,j);
n = GetMax(l,m); //Notice here we don't declare the type
cout << k << endl;
cout << n << endl;
}
#include <iostream>
using namespace std;
template <class T>
T GetMax(T a, T b)
{
T result;
result = (a>b)?a:b ;
return (result);
}
int main()
{
int i = 5, j = 6, k;
long l = 10, m = 5, n;
k = GetMax(i,j);
n = GetMax(l,m); //Notice here we don't declare the type
cout << k << endl;
cout << n << endl;
}
OUTOUT:
6
10
Notice how in this case, we called our function template Getmax() without explicitly specifying the type between angle-brackets <>. The compiler automatically determine what type is needed on each call.
Thursday, October 11, 2012
What is a CLASS
class is a keyword or mechanism which used to create user-defined datatype or we can say that a comprehensive.
A class defines the characteristics of an object.
characteristics include:
1. Attributes |car
(data fields --> properties) |[year, make, model, color, numbers of doors, engine]
2. behaviors |
(operation --> methods or fun.) |[on(), off(), changes(), accelerate(), turn(), break()]
public class Students
{
/*--code------*/
}
Students BiologyStd = new Students();
Students ComputerStd = new Students();
In our above example “Students” is a Class
which has objects “Biology Students” & “Computer Students”.
_____________________________________________________________________
Note: class in c# by default reference type while in c++ it is valued type.
Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:
- data type, which represents a blue-print of objects.
- A class is like a template.
A class defines the characteristics of an object.
characteristics include:
1. Attributes |car
(data fields --> properties) |[year, make, model, color, numbers of doors, engine]
2. behaviors |
(operation --> methods or fun.) |[on(), off(), changes(), accelerate(), turn(), break()]
Note: Here, Properties
means characteristic of object or entity
(thing).
Don't get confused Property which is a concept in oops.
______________________________________________________________________public class Students
{
/*--code------*/
}
Students BiologyStd = new Students();
Students ComputerStd = new Students();
In our above example “Students” is a Class
which has objects “Biology Students” & “Computer Students”.
_____________________________________________________________________
Note: class in c# by default reference type while in c++ it is valued type.
A class is composed of a set of data members and a set of
operations that can be performed on the data.
Class rectangle
{
private: | //we can use
private, public and protected, (internal
in c#)
int x; | set of data member
int y; | //data members are private by default
public: | this is set of
operation
void set_values (int, int);
| //1st function (only declaration)
int area () | //2nd function (with
definition)
{ | //function must be public
return (x*y); |
} |
};
In C++, a class type can be declared with the keywords union, struct, or class.
·
The members of a
class declared with the keyword class are private by default. A class is inherited
privately by default.
·
The members of a
class declared with the keyword struct are public by default. A structure is inherited
publicly by default.
·
The members of a
union (declared with the keyword union) are public by default. A union cannot be used as
a base class in derivation.
__________________________________________________________________________________
class X
{
/* define
class members here */
};
int main()
{
X xobject1; // create an object of class type X
X xobject2; //
create another object of class type X
}
You may have polymorphic classes in C++.
Polymorphism is the ability to use a function name that appears in different
classes (related by inheritance), without knowing exactly the class the
function belongs to, at compile time.
concepts related to C++ Classes
and Objects, discuss below
Concept
|
Description
|
Class member functions
|
A member function of a class is a function that has
its definition or its prototype within the class definition like any other
variable.
|
Class access modifiers
|
A class member can be defined as public, private or
protected. By default members would be assumed as private.
|
Constructor & destructor
|
A class constructor is a special function in a class
that is called when a new object of the class is created. A destructor is
also a special function which is called when created object is deleted.
|
C++ copy constructor
|
The copy constructor is a constructor which creates an
object by initializing it with an object of the same class, which has been
created previously.
|
C++ friend functions
|
A friend
function is permitted full access to private and protected members of a
class.
|
C++ inline functions
|
With an inline function, the compiler tries to expand
the code in the body of the function in place of a call to the function.
|
The this pointer in C++
|
Every object has a special pointer this which points to the object
itself.
|
Pointer to C++ classes
|
A pointer to a class is done exactly the same way a
pointer to a structure is. In fact a class is really just a structure with
functions in it.
|
Static members of a class
|
Both data members and function members of a class can
be declared as static.
|
·
Classes not only include information regarding
the real world object, but also functions to access the data, and classes
possess the ability to inherit from other classes.
·
A class
is an expanded concept of a data structure: instead of holding only data, it
can hold both data and functions.
·
An object
is an instantiation of a class. In terms of variables, a class would be the
type, and an object would be the variable.
·
Remember
: A class is a type, and an object of this class is just a variable.
Classes are generally declared using the keyword class, with the following format:
class class_name {
access_specifier_1: //please
notice : these colon
member1;
access_specifier_2:
member2;
...
} object_names;
Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, that can be either data or function declarations, and optionally access specifiers.
All is very similar to the declaration on data structures, except that we can now include also functions and members, but also this new thing called access specifier. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights that the members following them acquire:
·
private members of
a class are accessible only from within other members of the same class or from
their friends.
·
protected members are accessible from members of their
same class and from their friends, but also from members of their derived
classes.
·
Finally, public members are accessible from
anywhere where the object is visible.
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
|
Declares a class (i.e., a type) called CRectangle
and an object (i.e., a variable) of this class called rect.
This class contains four members: two data members of type int (member x and member y) with private access (because private is the default
access level) and two member functions with public access: set_values() and area(), of
which for now we have only included their declaration, not their definition.
//
classes example
#include
<iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void set_values (int,int); //prototype
int area ()
{
return (x*y);
}
};
void CRectangle::set_values (int a, int b) {
x = a;
y = b;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
|
The most important new thing in this code is the operator of scope (::, two
colons) included in the definition of set_values().
It is used to define a member of a class from outside the class definition
itself.
You may notice that the definition of the member function area() has been included directly within the definition of the CRectangle class given its extreme simplicity, whereas set_values() has only its prototype declared within the class, but its definition is outside it. In this outside definition, we must use the operator of scope (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
You may notice that the definition of the member function area() has been included directly within the definition of the CRectangle class given its extreme simplicity, whereas set_values() has only its prototype declared within the class, but its definition is outside it. In this outside definition, we must use the operator of scope (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
The only difference between defining a class member function
completely within its class or to include only the prototype and later its
definition, is that in the first case the function will automatically be
considered an inline member function by the compiler, while in the second it
will be a normal (not-inline) class member function, which in fact supposes no
difference in behavior.
Interview question: why u want to make class?
Answer: 1). better organization of data.
--give its a logical entity
--In class we all declared variable
have a continues memory location
--class gives us grouping of variable in one place.
--by organise we achieve
–maintainbility, security, efficiency.
2). Safety of data (private or protected)
3). Reusability (via inheritance)
4). Polymorphism (see above)
5). abstraction
Analogy:
If
a class is a house, then the functions will be the doors and the variables will
be the items inside the house.
Class help us to achieve encapsulation:
If a class is a house, then the functions will be the doors and the
variables will be the items inside the house. The functions usually will be the
only way to modify the variables in this structure, and they are usually the
only way even to access the variables in this structure. This might seem silly
at first, but the idea to make programs more modular - the principle itself is
called "encapsulation". The key idea is that the outside world
doesn't need to know exactly what data is stored inside the class--it just
needs to know which functions it can use to access that data. This allows the
implementation to change more easily because nobody should have to rely on it
except the class itself.
Constructor and destructor:
Classes
must always contain two functions: a constructor and a destructor. The syntax
for them is simple: the class name denotes a constructor, a ~ before the class
name is a destructor.
When
the programmer declares an instance of the class, the constructor will be
automatically called. The only time the destructor is called is when the
instance of the class is no longer needed--either when the program ends, the
class reaches the end of scope, or when its memory is deallocated using delete
Keep in mind that neither constructors nor
destructors return arguments! This means you do not want to (and cannot) return
a value in them.
Note
that you generally want your constructor and destructor to be made public so
that your class can be created! The constructor is called when an object is
created, but if the constructor is private, it cannot be called so the object
cannot be constructed. This will cause the compiler to complain.
// example: class constructor
#include
<iostream>
using namespace std;
class CRectangle
{
int width, height;
public:
CRectangle (int,int);
int area ()
{
return (width*height);
}
};
CRectangle::CRectangle
(int a, int b)
{
width = a;
height = b;
}
int main ()
{
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
return 0;
}
You can see how neither the
constructor prototype declaration (within the class) nor the latter constructor
definition include a return value; not even void.
Question. What is the difference between “C++ class” and “C#
class” ?
Tuesday, September 11, 2012
How to make abstract class in c++
C++ has no abstract keyword or equivalent, to make a class abstract is to declare a pure virtual function in it (which prevents instantiation).
For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.
C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.
What is a Pure Virtual Function:
A Pure Virtual Function is a Virtual function with no body.
Declaration of Pure Virtual Function:
Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.
Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};
class Demo1:public Sandeep
{
public:
void example()
{
cout << " \n Welcome";
}
};
class Demo2:public Sandeep
{
public:
void example()
{
cout << " To My blog \n";
}
};
int main()
{
Sandeep* arra[2];
Demo1 d1;
Demo2 d2;
arra[0]=&d1;
arra[1]=&d2;
arra[0]->example();
arra[1]->example();
}
OUTPUT:

For this same reason, in C++ there is not much difference between an interface and an abstract class - a C++ interface is simply a class containing only pure virtual functions.
C++ has no keyword abstract. However, you can write pure virtual functions; that's the C++ way of expressing abstract classes.
What is a Pure Virtual Function:
A Pure Virtual Function is a Virtual function with no body.
Declaration of Pure Virtual Function:
Since pure virtual function has no body, the programmer must add the notation =0 for declaration of the pure virtual function in the base class.
Syntax of Pure Virtual Function:
class class_name //This denotes the base class of C++ virtual function
{
public:
virtual void virtualfunctioname() = 0 //This denotes the pure virtual function in C++
};
___________________________________________________________________
//Full code example
#include <iostream>
using namespace std;
class Sandeep
{
public:
virtual void example()=0; //Denotes pure virtual Function Definition
};
class Demo1:public Sandeep
{
public:
void example()
{
cout << " \n Welcome";
}
};
class Demo2:public Sandeep
{
public:
void example()
{
cout << " To My blog \n";
}
};
int main()
{
Sandeep* arra[2];
Demo1 d1;
Demo2 d2;
arra[0]=&d1;
arra[1]=&d2;
arra[0]->example();
arra[1]->example();
}
OUTPUT:
Monday, August 20, 2012
Monday, July 2, 2012
Relation between class_Size and inheritance
#include <iostream>
using namespace std;
class Base
{
int first_int; //size of int is 4 byte
int second_int; //so size of Base class is 8 byte
};
class derived:public Base
{
int derived_member;
int another_derived_member;
int new_member;
};
class home
{
Base base_1;
Base base_2;
};
int main()
{
cout << sizeof(derived) << endl; //the size of derived class is (8+12=20) byte
cout << sizeof(Base) << endl;
cout << sizeof(home) << endl; //the size of home class is (8+8)=16 byte
}
OUTPUT:
20
8
16
using namespace std;
class Base
{
int first_int; //size of int is 4 byte
int second_int; //so size of Base class is 8 byte
};
class derived:public Base
{
int derived_member;
int another_derived_member;
int new_member;
};
class home
{
Base base_1;
Base base_2;
};
int main()
{
cout << sizeof(derived) << endl; //the size of derived class is (8+12=20) byte
cout << sizeof(Base) << endl;
cout << sizeof(home) << endl; //the size of home class is (8+8)=16 byte
}
OUTPUT:
20
8
16
Friday, June 8, 2012
Difference between class and struct in cpp
1) Members of a class are private by default and members of struct are public by default. For example program 1 fails in compilation and program 2 works fine.
// Program 1 to show we can't access the class data directly
#include <iostream>
using namespace std;
class Demo
{
int x; // x is private (by default)
};
int main()
{
Demo d;
d.x = 20; // compiler error because x is private
cout << d.x ;
}
// Program 2 to show we can access struct data directly
#include <iostream>
using namespace std;
struct Demo
{
int x; // x is public (by default)
};
int main()
{
Demo d;
d.x = 20; // works fine because x is public
cout << d.x ;
}
2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.
// Program 3 to show inheritance between classes is, private by-default
#include <iostream>
using namespace std;
class Base
{
public:
int x;
};
class Derived : Base { }; // is equilalent to class Derived : private Base {}
int main()
{
Derived d;
d.x = 20; // compiler error becuase inheritance is private
cout << d.x ;
}
// Program 4 to show inheritance between structure is, public by-default
#include <iostream>
using namespace std;
struct Base
{
public:
int x;
};
struct Derived : Base { }; // is equilalent to struct Derived : public Base {}
int main()
{
Derived d;
d.x = 20; // works fine becuase inheritance is public
cout << d.x ;
}
Note: struct in c# doesn't support inheritance.
// Program 1 to show we can't access the class data directly
#include <iostream>
using namespace std;
class Demo
{
int x; // x is private (by default)
};
int main()
{
Demo d;
d.x = 20; // compiler error because x is private
cout << d.x ;
}
// Program 2 to show we can access struct data directly
#include <iostream>
using namespace std;
struct Demo
{
int x; // x is public (by default)
};
int main()
{
Demo d;
d.x = 20; // works fine because x is public
cout << d.x ;
}
2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.
// Program 3 to show inheritance between classes is, private by-default
#include <iostream>
using namespace std;
class Base
{
public:
int x;
};
class Derived : Base { }; // is equilalent to class Derived : private Base {}
int main()
{
Derived d;
d.x = 20; // compiler error becuase inheritance is private
cout << d.x ;
}
// Program 4 to show inheritance between structure is, public by-default
#include <iostream>
using namespace std;
struct Base
{
public:
int x;
};
struct Derived : Base { }; // is equilalent to struct Derived : public Base {}
int main()
{
Derived d;
d.x = 20; // works fine becuase inheritance is public
cout << d.x ;
}
Note: struct in c# doesn't support inheritance.
Tuesday, May 8, 2012
Program to understand array of object
#include <iostream>
using namespace std;
class info
{
char name[20];
int roll;
public:
void getinfo()
{
cout << "\n Enter name: ";
cin >> name;
cout << "\nEnter roll number: ";
cin >> roll;
}
void displayinfo()
{
cout << "\n\n\tName: " << name;
cout << "\n\tRollNumber:" << roll;
}
};
int main()
{
info obj[5]; //Here i am creating 5 object using array
for(int i=0; i<5; i++)
{
obj[i].getinfo();
}
for(int i=0; i<5; i++)
{
obj[i].displayinfo();
}
cout << "\n\n";
}
using namespace std;
class info
{
char name[20];
int roll;
public:
void getinfo()
{
cout << "\n Enter name: ";
cin >> name;
cout << "\nEnter roll number: ";
cin >> roll;
}
void displayinfo()
{
cout << "\n\n\tName: " << name;
cout << "\n\tRollNumber:" << roll;
}
};
int main()
{
info obj[5]; //Here i am creating 5 object using array
for(int i=0; i<5; i++)
{
obj[i].getinfo();
}
for(int i=0; i<5; i++)
{
obj[i].displayinfo();
}
cout << "\n\n";
}
Friday, April 20, 2012
A simple program to understand classes in C#
using System;
class building
{
public int floors; //number of floors //C# class doesn't support 'public :'
public int area; //total square footage of building
public int occupants; //number of occupants
}
class building_Demo
{
public static void Main()
{
building house = new building(); //create a bbuilding object
int areaPP; //area per person
//assign values to fields to fields in house
house.occupants = 4;
house.area = 2500;
house.floors = 2;
//compute the area per person
areaPP = house.area/house.occupants;
Console.WriteLine("house has:\n "+
house.floors + " floors\n "+
house.occupants + " occupants\n "+
house.area + " total area\n "+
areaPP + " area per person");
}
}
OUTPUT:
house has:
2 floors
4 occupants
2500 total area
625 area per person
class building
{
public int floors; //number of floors //C# class doesn't support 'public :'
public int area; //total square footage of building
public int occupants; //number of occupants
}
class building_Demo
{
public static void Main()
{
building house = new building(); //create a bbuilding object
int areaPP; //area per person
//assign values to fields to fields in house
house.occupants = 4;
house.area = 2500;
house.floors = 2;
//compute the area per person
areaPP = house.area/house.occupants;
Console.WriteLine("house has:\n "+
house.floors + " floors\n "+
house.occupants + " occupants\n "+
house.area + " total area\n "+
areaPP + " area per person");
}
}
OUTPUT:
house has:
2 floors
4 occupants
2500 total area
625 area per person
Thursday, April 5, 2012
A simple 'class' program
//class with single object
// using c++
#include <iostream>
using namespace std;
class CRectangle //here we define a user-defined-datatype 'CRectangle' using 'class'
{
private:
int x, y;
public:
void set_values (int,int);
int area()
{
return (x*y);
}
};
void CRectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
CRectangle rect; //creating the object (rect) of a class (CRectangle)
rect.set_values (3,4);
cout << "area:" << rect.area();
cin.ignore();
cin.get();
}
// using c++
#include <iostream>
using namespace std;
class CRectangle //here we define a user-defined-datatype 'CRectangle' using 'class'
{
private:
int x, y;
public:
void set_values (int,int);
int area()
{
return (x*y);
}
};
void CRectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
CRectangle rect; //creating the object (rect) of a class (CRectangle)
rect.set_values (3,4);
cout << "area:" << rect.area();
cin.ignore();
cin.get();
}
Note: we must use the operator scope-resulation (::) to specify that we are defining a function that is a member of the class CRectangle and not a regular global function.
_________________________________________________________
See also the same program using struct (only difference, we use 'struct' instead of 'class')
_________________________________________________________
See also the same program using struct (only difference, we use 'struct' instead of 'class')
Subscribe to:
Posts (Atom)