C++ Program For Classes and Objects
Free C++ course with real-time projects Start Now!!
Program 1
// Program for Class and Object #include<iostream> #define clrscr() system("cls") using namespace std; class studentData { // Property of class private: int rno; char name[50]; int mobile; //methods public: void getData(); void display(); }; void studentData::getData() { cout<<"Enter Roll No"; cin>>rno; cout<<"\nEnter Name"; cin>>name; cout<<"\nEnter mobile no"; cin>>mobile; } void studentData::display() { cout<<"\nRoll No: "<<rno; cout<<"\nName: "<<name; cout<<"\nMobile No: "<<mobile; } int main() { studentData sd; clrscr(); sd.getData(); sd.display(); return 0; }
Program 2
// Program for Class and Object1 #include<iostream> #define clrscr() system("cls") using namespace std; class Car { int model,year; char name[40],color[30]; public: void getCardetails(); void displaydetails(); }; void Car::getCardetails() { cout<<"Enter Car Name"; cin>>name; cout<<"Enter car Color"; cin>>color; cout<<"Enter car model no"; cin>>model; cout<<"Enter Year :"; cin>>year; } void Car::displaydetails() { cout<<"\nCar Name: "<<name; cout<<"\nCar Color: "<<color; cout<<"\nCar model no"<<model; cout<<"\nYear :"<<year; } int main() { Car C1; clrscr(); C1.getCardetails(); C1.displaydetails(); return 0; }
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google