0% found this document useful (0 votes)
24 views4 pages

C++ Program

The document discusses key concepts of classes and objects in C++ including: 1. Defining a Room class with data members for length, breadth, height and member functions to calculate area and volume. An object is created to access these. 2. Defining a Geeks class with public data member geekname and member function printname(). An object is created and these are accessed. 3. Defining a function printname() outside the class and accessing it using an object. 4. Demonstrating default and parameterized constructors and calling them when objects are created. 5. Demonstrating destructors and seeing them called when objects go out of scope.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
24 views4 pages

C++ Program

The document discusses key concepts of classes and objects in C++ including: 1. Defining a Room class with data members for length, breadth, height and member functions to calculate area and volume. An object is created to access these. 2. Defining a Geeks class with public data member geekname and member function printname(). An object is created and these are accessed. 3. Defining a function printname() outside the class and accessing it using an object. 4. Demonstrating default and parameterized constructors and calling them when objects are created. 5. Demonstrating destructors and seeing them called when objects go out of scope.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 4

// Program to illustrate the working of

// objects and class in C++ Programming

#include <iostream>
using namespace std;

// create a class
class Room {

public:
double length;
double breadth;
double height;

double calculateArea() {
return length * breadth;
}

double calculateVolume() {
return length * breadth * height;
}
};

int main() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculateArea() << endl;
cout << "Volume of Room = " << room1.calculateVolume() << endl;

return 0;
}

// C++ program to demonstrate accessing of data members

#include <bits/stdc++.h>

using namespace std;


class Geeks {
// Access specifier
public:
// Data Members
string geekname;
// Member Functions()
void printname() { cout << "Geekname is:" << geekname; }
};
int main()
{
// Declare an object of class geeks
Geeks obj1;
// accessing data member
obj1.geekname = "Abhi";
// accessing member function
obj1.printname();
return 0;
}

/ C++ program to demonstrate function


// declaration outside class

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
string geekname;
int id;

// printname is not defined inside class definition


void printname();

// printid is defined inside class definition


void printid()
{
cout <<"Geek id is: "<<id;
}
};

// Definition of printname using scope resolution operator ::


void Geeks::printname()
{
cout <<"Geekname is: "<<geekname;
}
int main() {

Geeks obj1;
obj1.geekname = "xyz";
obj1.id=15;

// call printname()
obj1.printname();
cout << endl;

// call printid()
obj1.printid();
return 0;
}

// C++ program to demonstrate constructors


#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
int id;

//Default Constructor
Geeks()
{
cout << "Default Constructor called" << endl;
id=-1;
}

//Parameterized Constructor
Geeks(int x)
{
cout <<"Parameterized Constructor called "<< endl;
id=x;
}
};
int main() {

// obj1 will call Default Constructor


Geeks obj1;
cout <<"Geek id is: "<<obj1.id << endl;

// obj2 will call Parameterized Constructor


Geeks obj2(21);
cout <<"Geek id is: " <<obj2.id << endl;
return 0;
}
// C++ program to explain destructors
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
public:
int id;

//Definition for Destructor


~Geeks()
{
cout << "Destructor called for id: " << id <<endl;
}
};

int main()
{
Geeks obj1;
obj1.id=7;
int i = 0;
while ( i < 5 )
{
Geeks obj2;
obj2.id=i;
i++;
} // Scope for obj2 ends here

return 0;
} // Scope for obj1 ends here
-----------------------------------------------------------------------------------
-------------------------------------------------------
#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
};
int main() {
Student s1; //creating an object of Student
s1.id = 201;
s1.name = "Sonoo Jaiswal";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
void insert(int i, string n)
{
id = i;
name = n;
}
void display()
{
cout<<id<<" "<<name<<endl;
}
};
int main(void) {
Student s1; //creating an object of Student
Student s2; //creating an object of Student
s1.insert(201, "Sonoo");
s2.insert(202, "Nakul");
s1.display();
s2.display();
return 0;
}

You might also like