Wednesday 28 March 2018

Classes and Objects

Class :

Class is a user defined data type which is a collection of data members and member function together.

Wrapping of data members and member function together in a single unit is called as class.

Data Members gives the information about object and member function gives the operation on the object or it describes the behavior of the object.

Class helps you for data abstraction as well as for the data encapsulation which gives the advantage of data hiding.

       Syntax of Class Definition:
       class classname
       {
              private:
              //variable declaration;
              public:
              // variable declaration;
              //function declaration;
         };
The keyword 'class' indicates the name of the class. The body of the class enclosed with curly braces followed by semicolon.

The Body of class contain declaration of variables and function collectively called as data members and member function

Class helps you to apply access specifies on data members and member functions.

Data members and member function specified by private access specifies are accessible only to their own class members and not to outsiders but on the other hand public members are accessed to their own members and also from outside class.

Members in the without access specifies are private by default.

Access Specifies public, private are followed by colon.


 
Creating Object :
Declaration of class does not define any objects but specifies what they will contain.Creating object of class called as creating variables of the class. Object of class can be created by using class name like any other variable.

The Syntax is :
 Class_name object_name;
OR
Class_name object_name1,object_name2,object_name3,.........object_name n;

The Memory for the object is created when it is created.

Ex.
       Student xyz;
OR  Student xyz, abc,......;

Objects can also be created when the class is defined by placing their names immediately after the closing brace.

class Student
{
   //Data Members
  // Member Functions
}xyz,abc;

Accessing Class Members :

Once an object of a class has been created , There must provision to access its members. This is achieved by using member access operator that is (.) dot.

The syntax for Accessing Data Members of a class :

Object_name.data_member;

Syntax for accessing Member Function of class :

Object_name.function_name(Actual Arguments);

0 Comments:

Post a Comment

Popular Posts

Categories

AI (27) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (114) C (77) C# (12) C++ (82) Course (60) Coursera (176) coursewra (1) Cybersecurity (22) data management (11) Data Science (89) Data Strucures (6) Deep Learning (9) Django (6) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (5) flutter (1) FPL (17) Google (19) Hadoop (3) HTML&CSS (46) IBM (25) IoT (1) IS (25) Java (92) Leet Code (4) Machine Learning (44) Meta (18) MICHIGAN (5) microsoft (3) Pandas (3) PHP (20) Projects (29) Python (741) Python Coding Challenge (192) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (40) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses