C++ Classes and Objects

Master C++ with Real-time Projects and Kickstart Your Career Start Now!!

Program 1

// Class and Object 
#include<iostream>
using namespace std;
class Student
{
    private:
    int rno;
    string name;
    string course;
    double cgpa;
   public:
     void getData();
     void putData(); 
};
void Student::getData()
{
    cout<<"\nEnter Roll No: ";
    cin>>rno;
    cout<<"\nEnter Name: ";
    cin>>name;
    cout<<"\nEnter Course: ";
    cin>>course;
    cout<<"\n Enter CGPA: ";
    cin>>cgpa;
}
void Student::putData()
{
    cout<<"\nRoll No: "<<rno;
    cout<<"\nName: "<<name;
    cout<<"\nCourse: "<<course;
    cout<<"\n CGPA: "<<cgpa;
}

int main()
{
    system("cls");
    // Create object
    Student S;  // object
    S.getData();
    S.putData();
    return 0;
}

Program 2

// Class and Object 
#include<iostream>
using namespace std;
class Car
{
    private:
    string carname;
    int modelyear;
    int price;
    string color;
    string trans;
  public:
  void getData();
  void putData();  
};

void Car:: getData()
{
    cout<<"\n Enter car name: ";
    cin>>carname;
    cout<<"\n Enter model year : ";
    cin>>modelyear;
    cout<<"Enter car price:(in Lakhs) ";
    cin>>price;
    cout<<"Enter color: ";
    cin>>color;
    cout<<"Enter Transmission: ";
    cin>>trans;
}
void Car::putData()
{
     cout<<"\n-------------------------------------------------------";
     cout<<"\n Name \t Model \t Price \t Color \t Transmission";
     cout<<"\n-------------------------------------------------------";
     cout<<"\n"<<carname<<" \t"<<modelyear<<" \t"<<price<<" \t"<<color<<" \t"<<trans;
}

int main()
{
    system("cls");
    Car C1;
    C1.getData();
    C1.putData();
    return 0;
}

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *