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

C++ Class and Object Examples

Uploaded by

mteverest1983
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

C++ Class and Object Examples

Uploaded by

mteverest1983
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

/* C++ program to create a simple class and object.

*/

#include <iostream>
using namespace std;

class Hello
{
public:
void sayHello()
{
cout << "Hello World" << endl;
}
};

int main()
{
Hello h;

[Link]();

return 0;
}

/* C++ program to create an object of a class


and access class attributes */

#include <iostream>
#include <string>
using namespace std;

// class definition
// "student" is a class
class Student {
public: // Access specifier
int rollNo; // Attribute (integer variable)
string stdName; // Attribute (string variable)
float perc; // Attribute (float variable)
};

int main()
{
// object creation
Student std;

// Accessing attributes and setting the values


[Link] = 101;
[Link] = "Shivang Yadav";
[Link] = 98.20f;

// Printing the values


cout << "Student's Roll No.: " << [Link] << "\n";
cout << "Student's Name: " << [Link] << "\n";
cout << "Student's Percentage: " << [Link] << "\n";

return 0;
}

/* C++ program to create multiple objects of a class */

#include <iostream>
#include <string>
using namespace std;

// class definition
// "student" is a class
class Student {
public: // Access specifier
int rollNo;
string stdName;
float perc;
};

int main()
{
// multiple object creation
Student std1, std2;

// Accessing attributes and setting the values


[Link] = 101;
[Link] = "Shivang Yadav";
[Link] = 98.20f;
[Link] = 102;
[Link] = "Hrithik Chandra Prasad";
[Link] = 99.99f;

// Printing the values


cout << "student 1..."
<< "\n";
cout << "Student's Roll No.: " << [Link] << "\n";
cout << "Student's Name: " << [Link] << "\n";
cout << "Student's Percentage: " << [Link] << "\n";

cout << "student 2..."


<< "\n";
cout << "Student's Roll No.: " << [Link] << "\n";
cout << "Student's Name: " << [Link] << "\n";
cout << "Student's Percentage: " << [Link] << "\n";

return 0;
}

/* C++ program to define a class method


outside the class definition*/

#include <iostream>
using namespace std;

// class definition
// "Sample" is a class
class Sample {
public: // Access specifier
// method declarations
void printText1();
void printText2();
void printValue(int value);
};

// Method definitions outside the class


// method definition 1
void Sample::printText1()
{
cout << "[Link]\n";
}

// method definition 2
void Sample::printText2()
{
cout << "Let's learn together\n";
}

// method definition 3
// it will accept value while calling and print it
void Sample::printValue(int value)
{
cout << "value is: " << value << "\n";
}

int main()
{
// creating object
Sample obj;

// calling methods
obj.printText1();
obj.printText2();
[Link](101);

return 0;
}

You might also like