UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Class and Objects C++

Ashish Upadhyay
17/05/2017 0 0

CLASS AND OBJECT IN C++ WITH EXAMPLE

FOUNDATION

Lets define a variable  x of integer type and give it a value 12. How will you do it. You will simply write

int x=12;

This statement can also be written as
int x;

x=12;

Here  int is a primitive data type in C++  x is a variable of integer type and the statement x=12; assigns a value of 12 to the variable x of integer type

CLASS

A class can be defined in many ways as mentioned below

  1. A class is a user defined data structure.
    2. A class is a programming construct.
    3. In C++ Classes are defined as data structures that contain variables and functions.


The variables defined in the class are also called as data members or fields or variables. The functions defined in the class are also called as member functions or methods or functions.

Note: Functions are used to manipulate the value of the variables.

Classes enable you to create your own custom, self-contained, and reusable types.

A class file is considered a blueprint of objects that we use in our code. 

We create class files to help model real-world objects in the code.

class in C++ is a user defined type or data structure declared with keyword class that has data and functions  as its members whose access is governed by the three access specifiers:  private, protected or public (by default access to members of a class is private) 

Class Members:

The functions and variables defined in the class are called Class Members.

“Consider a software that manages a bank ATM.  To create the software a programmer needs to understand the concept of customers, accounts, transactions, etc.   It's far easier to implement the software application by modeling these real world objects as software objects (classes) in your code.”

  Creating Classes and Members

In C++, a class is a programming construct that you can use to define your custom types.

(A programming construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language. The term "language construct" is used as a synonym for control structure)  

When you create a class, you are effectively creating a blueprint for the type. So a class is also called as Blue Print of an Object. The class defines the behaviors and characteristics of its objet.

Behaviors are represented by defining functions in the class whereas characteristics are represented by defining variables within the class. 

Example:

Suppose you want to create a class to represent a rectangle shape in your program.  You use the class keyword to declare a class, as shown in the following example:

//Declaring a Class


class Rectangle
{
public:
    int width;
    int height;
};

Here we have declared a class called Rectangle and given it two public member variables called width and height, that will be used to represent the width and height of our rectangle.  

Note that they are accessible directly because they are public, as a result of the public: modifier.

 Using a Class

We have created a class to represent a rectangle, now we use that class to create instances of a rectangle in our program. 

When we create a new rectangle from this class, it is known as a rectangle object and will be given a unique name. 

 // How to use a Class

void main()
{
     Rectangle Big;
     Rectangle Small;     

    Big.width = 6
    Big.height = 8;

    Small.width = 2;
    Small.height = 3;
}

Here we have created two rectangle objects and called them Big and Small.  
Then, by using “dot notation" (or the dot operator), we have provided values for the width and height of each rectangle.  The Big rectangle is 6 x 8 and the Small rectangle is 2x3.

Note: Initialization is an important part of working with your classes in C++. 

Even when using intrinsic data types, if you do not initialize the variable for that type and you access it in your code, you will end up with whatever values are stored in the memory location that the variable refers to.  This is something you want to avoid doing.   You should always initialize your types in C++;

C++ offers a couple of options for initializing your classes.  You can initialize the member variables by using the dot operator and setting the values explicitly or you can include a constructor in your class that is responsible for initialization the member variables.

 EXAMPLE: CREATING A CLASS, OBJECT AND INSTANTIATION

class  Student // Creating  a class with name Student

{       
public:
            int age;

           String name;

};

void main()

{

clrscr();

Student  A; // A is an instance of the class Student

Student  B; // B is also an instance of class Student

 

/*Class Student has two class members: age and name. Every object of class Student must have some specific values for these two class members. Assigning value to the class members for each object is called Initialization*/

 

// Initialization for object A of class Student

A.age=33;
A.name=”Ashish”;

 

// Initialization for object B of class Student

B.age=30;

B.name=”Parvati”;

 

getch();

}

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Polymorphism and Virtual Functions
Polymorphism and Virtual Functions Compile Time Polymorphism or Early Binding or Static Binding An object is bound to its function call at compile time. Implemented using overloaded functions and...

Why Indexing Should Start From Zero In Array ?
Why numbering should start at zero? To denote the subsequence of natural numbers 2, 3, ..., 12 without the pernicious three dots, fourconventions are open to usa) 2 ≤ i < 13b) 1 < i ≤ 12c)...

Memory Layout in C++ vis-a-vis Polymorphism and Padding bits.
I know there is no need for the knowledge of memory layout for a normal day-to-day development of C++. However, when someone goes in the bit/byte level of C++, he needs to look for such discussion. To...

Macro v/s Inline Function
Disadvantages of Macro compared to functions: Works on text substituiton mechanism by the preprocessor unlike normal functions where assembly-language Call and Return are used. Preprocessor macros...

C++ Program[Working with structure]
//Header files #include<iostream>#include<cstring> //Namespace usedusing namespace std; //Structure definition struct store{ int p_id; char p_name; int quantity; float price; float amount; }; //...
S

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more