Académique Documents
Professionnel Documents
Culture Documents
CDAC MUMBAI
BASIC INPUT/OUTPUT
OUTPUT OPERATOR
Screen
.
..
cout
<<
C++
Object
Insertion
operator
Variable
INPUT OPERATOR
Extraction
operator
Cin
>>
------Keyboard
Variable
(number)
45.4
//30
//10;
//20;
NAMESPACE
DEFINING A NAMESPACE
namespace namespace_name
{
// Declarations of Variables,
// Functions ,Classes etc.
}
Example:
namespace Mynamespace
{
int m;
void display(int n)
{
cout<<n;
}
}
// no semicolon here
To access identifiers declared within namespace, we use scope resolution
operator (::) shown below
Mynamespace::m=100
Mynamespace::display(10);
USING NAMESPACE
CLASSES
Object:
An Object is a combination of data and behavior.
Object behavior is referred using terms such as
functions, member functions, methods or operations.
Data characterizes the state of an object.
Class:
Description of common properties of objects that
belong to the class i.e., collection of objects with
common characteristics.
A class does not exist in the memory of computer
during program execution.
e.g.: Writing material is a class. Pen and Pencil
are objects of the class.
CLASS
ATTRIBUTES OF CLASS
Member variables:
Member functions:
Constructors
Destructors
Static functions
Non-static
Const functions
Virtual functions
ACCESS MODIFIERS
Members can be declared
Private
visible only inside the class
Protected
private + visible to the immediately derived class
Public
globally visible
CLASS DECLARATION
CREATING OBJECTS
Once a class has been declared, we can create
variables of that type by using the class name.
Eg:
item x;
Item x, y, z;
class item
{ ..
}x,y,z;
object-name.function-name(actual-arguments);
CLASSES
Object is an instance of a class, and the process
of creating an object is called instantiation.
Function declared within the definition of a
class is called a member function or method.
Member function can be defined in two places:
CLASSES
Irrespective of the place of definition, the
function should perform the same task.
The code in both the case remains the
same, however the difference is the
function header defined.
1. Outside the class definition
member
The
};
INLINE FUNCTIONS
};
#include <iostream.h>
class set
{
private:
int m , n;
public :
void input(void);
void display(void);
int largest(void);
};
main(void)
{
set A;
A.input();
A.display();
}
member variable1
member variable2
object2
member variable2
member variable2
object3
member variable3
member variable2
x.show();
y.set(2,2);
y.show();
x.show();
return 0;
}
Note that the type and scope of each static
member variable must be defined outside the
class definition.
Object 2
y
2
a
Common to two objects
ARRAY OF OBJECTS
Consider class definition :
class employee
{
char name [30];
float age;
public:
void getdata(void);
void putdata(void);
};
The identifier employee is a user-defined data
type and can be used to create objects that relate
different categories of the employees.
ARRAY OF OBJECTS
employee manager[3];
employee clerk[23];
// array of manager
// array of clerk
ARRAY OF OBJECTS
name
age
name
Manager[0]
Manager[1]
age
name
age
Manager[2]
Constructors, Destructors
CONSTRUCTOR
Constructors are special member functions
with the same name as the class.
A constructor initializes each object when it
is created
They are commonly used to initialize the
member variables, without the need to make
a separate call to a member function.
CONSTRUCTOR CONTD..
TYPES OF CONSTRUCTORS
Default constructor
Parameterized constructors
Default argument constructors
Copy Constructors
Dynamic Constructors
void main()
{
integer i1(100),i2,i3;
i1.displaydata( );
i2.setdata(200);
i2.displaydata();
i3.getdata( );
/*300 is entered as I/p */
i3.displaydata( );
}
COPY CONSTRUCTORS
COPY CONSTRUCTORS
class sum
{
int x;
public:
sum(){ }
sum(int i) {x=i;}
sum(sum &j) {x=j.x;}
void displaydata() {
cout <<value of x = << x<<endl; }
};
void main()
{
sum s1(10);
sum s2(s1);
sum s3=s1;
sum s4;
s4=s1;
s1.displaydata();
s2.displaydata();
s3.displaydata();
s4.displaydata();
}
DYNAMIC CONSTRUCTORS
#include<string>
class String
{
char* data;
public:
String(const char* s = "")
{
data = new char[20];
strcpy(data,s);
}
~String() {
delete [] data;
}
int main()
{
String s = "hello";
String t = s; // same as String
t(s);
s.display( );
t.display( );
t.assign(world);
s.display( );
t.display( );
}
};
void display() {
cout << data;
PROPERTIES OF CONSTRUCTOR
PROPERTIES OF CONSTRUCTOR
DESTRUCTORS
overloaded
DESTRUCTORS
DESTRUCTORS
~example() // destructor (same name with tilde)
{
cout<<endl<<inside the destructor;
}
};
void main()
{
example e;
}
PROPERTIES OF DESTRUCTOR
FRIEND FUNCTIONS
FRIEND FUNCTIONS
#include <iostream>
using namespace std;
class CRectangle
{
FRIEND CLASS
#include <iostream>
class CSquare;
// pre-declaration
class CRectangle
{
int width, height;
public:
int area ()
{ return (width * height);
void convert (CSquare a);
};
class CSquare
{
private:
int side;
public:
void set_side (int a)
{
side=a; }
friend class CRectangle;
};