Home » Code Snippets » C/C++ Code Snippets

C++ Class Exercise - Read and Print Class, Students Details using Two Classes

In this C++ program we will learn how we can use one class’s object inside another class definition to read details of first class?

This program will read class details along with number of students in a class, so we will create student class first and then we will create classDetails class. Inside classDetails class we will use object of student class’s object.

C++ program - Read and print Class, Student details using Two Classes

Let’s consider the following example:

/*C++ Class Exercise - Read and Print Class, Students 
Details using Two Classes*/

#include <iostream>
#include <string.h>

using namespace std;

class student{
		char name[30];
		int rollNo;
		void getStudent(){
			strcpy(name,"PIYA KAUSHAL");
		void printStudent(){
			cout<<"Name: "<<name<<",Roll No.: "<<rollNo<<endl;

class classDetails{
		char clsName[30];
		student std; //object
		void getClassDetails(){
			strcpy(clsName,"Higher Sec.");
		void printClassDetails(){
			cout<<"Class Name: "<<clsName<<endl;

int main()
	classDetails CD;
	return 0;


    Class Name: Higher Sec.
    Name: PIYA KAUSHAL,Roll No.: 101

Comments and Discussions

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.