2. C++ is a multi-paradigm programming language. Meaning, it
supports different programming styles.
One of the popular ways to solve a programming problem is
by creating objects, known as object-oriented style of
programming.
C++ Class
Before you create an object in C++, you need to define a
class.
A class is a blueprint for the object.
We can think of class as a sketch (prototype) of a house. It
contains all the details about the floors, doors, windows
etc. Based on these descriptions we build the house. House
is the object.
As, many houses can be made from the same description,
we can create many objects from a class.
3. Class: The building block of C++ that leads to Object
Oriented programming is a Class. It is a user defined
data type, which holds its own data members and
member functions, which can be accessed and used
by creating an instance of that class. A class is like a
blueprint for an object.
For Example: Consider the Class of Cars. There may
be many cars with different names and brand but all
of them will share some common properties like all
of them will have 4 wheels, Speed Limit, Mileage
range etc. So here, Car is the class and wheels, speed
limits, mileage are their properties.
.
4. A Class is a user defined data-type which has data
members and member functions.
Data members are the data variables and member
functions are the functions used to manipulate these
variables and together these data members and
member functions defines the properties and behavior
of the objects in a Class.
In the above example of class Car, the data member
will be speed limit, mileage etc and member functions
can be apply brakes, increase speed etc
An Object is an instance of a Class. When a class is
defined, no memory is allocated but when it is
instantiated (i.e. an object is created) memory is
allocated.
5. A class is defined in C++ using keyword class
followed by the name of class.
The body of class is defined inside the curly
brackets and terminated by a semicolon at the end.
class className
{
// some data
// some functions
};
6. A class is defined in C++ using keyword class
followed by the name of class. The body of class
is defined inside the curly brackets and
terminated by a semicolon at the end.
7. Declaring Objects: When a class is defined, only the
specification for the object is defined; no memory or
storage is allocated. To use the data and access
functions defined in the class, you need to create
objects.
Syntax:
ClassName ObjectName; Accessing data members and
member functions: The data members and member
functions of class can be accessed using the dot(‘.’)
operator with the object. For example if the name of
object is obj and you want to access the member
function with the name printName() then you will have
to write obj.printName() .
8. The public data members are also accessed in the
same way given however the private data members
are not allowed to be accessed directly by the
object.
Accessing a data member depends solely on the
access control of that data member.
This access control is given by Access modifiers in
C++. There are three access modifiers : public,
private and protected.
9. class Test
{ private:
Int data1;
float data2;
public:
void function1()
{ data1 = 2; }
float function2()
{
data2 = 3.5;
return data2;
}
};
Here, we defined a class named Test.
This class has two data members: data1 and data2 and two member
functions: function1() and function2()
10. The private keyword makes data and
functions private. Private data and functions
can be accessed only from inside the same
class.
The public keyword makes data and
functions public. Public data and functions
can be accessed out of the class.
Here, data1 and data2 are private members
where as function1() and function2() are
public members.
If you try to access private data from outside
of the class, compiler throws error. This
feature in OOP is known as data hiding.
11. When class is defined, only the specification
for the object is defined; no memory or
storage is allocated.
To use the data and access functions defined
in the class, you need to create objects.
Syntax to Define Object in C++
className objectVariableName; You can
create objects of Test class (defined in above
example) as follows:
12. You can access the data members and member
functions by using a . (dot) operator. For
example,
o2.function1(); This will call the function1()
function inside the Test class for objects o2.
Similarly, the data member can be accessed as:
o1.data2 = 5.5; It is important to note that, the
private members can be accessed only from inside
the class.
So, you can use o2.function1(); from any function
or class in the above example. However, the code
o1.data2 = 5.5; should always be inside the class
Test.
13. In this program, two data members data1 and data2 and two
member functions insertIntegerData() and insertFloatData()
are defined under Test class.
Two objects o1 and o2 of the same class are declared.
The insertIntegerData() function is called for the o1 object
using:
o1.insertIntegerData(12); This sets the value of data1 for
object o1 to 12.
Then, the insertFloatData() function for object o2 is called and
the return value from the function is stored in variable
secondDataOfObject2 using:
secondDataOfObject2 = o2.insertFloatData(); In this program,
data2 of o1 and data1 of o2 are not used and contains garbage
value.
You should also check these topics to learn more on objects
14. Void-the data that is not returned
Main function theke class er object create kore sei
object er madhdhome public
function(insertintegerdata) k call kore sei class er
private member e value (int d)assign kora hoy.
Erpor, float e main function theke user input nei.
Then value return kori
Erpor float e data return kore main e rakha hoy
Test-classname
O1 o2-object name
Cout-console screen e print f
Cin-scanf
15.
16. cout is an object of the output stream that is
used to show output. Basically, cin is an
input statement while cout is an output
statement. They also use different
operators. cin uses the insertion operator( >>
) while cout uses the extraction operator( <<
).Feb 15, 2018
17. endl, among other things, will flush the
stream, i.e., empty it to whatever target it
has (screen, file, ...). So...if you just want to
end the line you can do either. But if you use
cout I suggest you stick with the manipulator
cout since it will also flush your stream. Jan
30, 2009 at 1:33am.Jan 30, 2009
18. A constructor is a special type of member
function that initialises an object automatically
when it is created.
Compiler identifies a given member function is a
constructor by its name and the return type.
Constructor has the same name as that of the class
and it does not have any return type. Also, the
constructor is always public.
... .. ... class temporary { private: int x; float y;
public: // Constructor temporary(): x(5), y(5.5) { //
Body of constructor } ... .. ... }; int main() {
Temporary t1; ... .. ... } Above program shows a
constructor is defined without a return type and
the same name as the class.
19. Constructor can be overloaded in a similar way
as function overloading.
Overloaded constructors have the same name
(name of the class) but different number of
arguments.
Depending upon the number and type of arguments
passed, specific constructor is called.
Since, there are multiple constructors present,
argument to the constructor should also be passed
while creating an object.
20. For object A1, no argument is passed while creating the
object.
Thus, the constructor with no argument is invoked
which initialises length to 5 and breadthto 2. Hence,
area of the object A1 will be 10.
For object A2, 2 and 1 are passed as arguments while
creating the object.
Thus, the constructor with two arguments is invoked
which initialises length to l (2 in this case)
and breadth to b (1 in this case). Hence, area of the
object A2 will be 2.
Output
Default Area when no argument is passed. Area: 10 Area
when (2,1) is passed as argument. Area: 2