You must Sign In to post a response.
  • Category: Programming

    Basic program using concept of class, object using C++.


    Preparing for C++ exam and facing doubt in writing a program? Wondering about how to write a basic program using class and objects? Scroll through this page for solutions from our ISC experts.

    Following is a question that was asked in the question paper of B.E. Mechanical Engineering (June/July 2017)conducted by Bangalore University, for subject Object Oriented Programming(2K11 Scheme)(ME-601), Semester-VI

    This question is asked for 10 marks and hence requires some explanation. Please let me know the solution.

    Q) Define class and object. Write a class containing two data items length and breadth and three functions getdata(), displaydata() and area(). Write a main program which declare the object and uses the member functions of the class. [10 Marks]
  • Answers

    2 Answers found.
  • Class and object are very basic and important concepts of Object Oriented Programming.
    Class :- Class is a user defied data type which behaves like built in data type of programming language. Class consists of member variables and member functions. Member functions which operates on member variables of the class. Member variables and member functions are wrapped in a single unit called as class and this wrapping up of data and function is called as encapsulation.
    Object:- As every data type has its own variables of that type, class is also having variable of type class which are commonly known as objects. So object is the run time entity of type class. Object occupies storage in the memory.

    Below is the program which is prepared by using above mentioned basic concepts of Object Oriented Programming.
    Please include and header files before declaration of class.

    #include
    #include
    class sample
    {
    private:
    float l,b,a;
    public:
    void getdata(void)
    {
    cout<<"Enter length and breadth of rectangle in centimeter\n";
    cin>>l>>b;
    }
    void displaydata(void)
    {
    cout<<"\nLength of reactangle is "< cout<<"\nBreadth of rectangle is "< a=l*b;
    }
    void area(void)
    {
    cout<<"\nArea of rectangle is "< }
    };
    void main(void)
    {
    class sample s;
    clrscr();
    s.getdata();
    s.displaydata();
    s.area();
    getch();
    }

    recarea-program-output.docx

    Delete Attachment

  • OOP :- OOP stands for Object Oriented Programming. The main aim of OOP is to deal with real world entity. Before OOP programs were written in Procedural Programming. In procedural programming we deal with the function or procedures that perform operation on data but in OOP we deal with object that contain both data and functions. There is no of listed features of OOP:
    (A) - Class
    (B) - Object
    (C) - Abstraction
    (D) - Encapsulation
    (E) - Polymerphism
    (F) - Inhertiance

    Class -: Class is an important feature of OOP. Class is just like a container that contain data member and member function. Meaning of data member is data variables and meaning of member function is function which perform the operation on the data variables. There are various ways to declare the class. To declare the class we need to use the class keyword. Class is also declare with access modifiers(like public, private,etc.)

    Object -: Object is one of the important feature of OOP. Object is the real world entity that have state and behaviour. Object is the instance of the class. No memory is allocated when a class is created but when an object is created for the class at that time memory is allocated. To declare an object we need to give class name.
    For Ex. className obj;
    Now suppose your className have three functions or member functions like getdata(), displaydata() and area() and you want to access these functions from out of the class then you need to create the object of that particular class that we already created but here one thing which we need to keep in our mind during the declaration of function if you want to access those functions out of the class you need to declare those function as public. Now below is the procedure to access those functions out of the class :
    obj.getdata();
    obj.displaydata();
    obj.area();

    Below is the example of the class and objects:-

    #include
    using namespace std;
    class IndiaStudyChannel
    {
    private:
    float length, breadth, areaOf;
    public:
    void getdata()
    {
    cout<<"Enter the length :";
    cin>>length;
    cout<<"\nEnter the breadth :";
    cin>>breadth;
    }
    void displaydata()
    {
    cout<<"\nEntered length : "< cout<<"\nEntered breadth : "< }
    void area()
    {
    areaOf = length * breadth;
    cout<<"\nCalculated area : "< }
    };

    int main() {
    // your code goes here
    IndiaStudyChannel obj;
    obj.getdata();
    obj.displaydata();
    obj.area();
    return 0;
    }


  • Sign In to post your comments