Simple class example of c++ for beginners
This class maintain a library card catalog entry.Have the class store a book's title,author,and the number of copies available.
#include<iostream>
using namespace std;
class card
{
private:
char booksTitle[50],author[50];
int av_copies;
public:
set_a()
{
cout<<"Enter the Books Title:" << endl;
cin>> booksTitle;
cout<<"Author:" << endl;
cin>> author;
cout<<"Available copies:" << endl;
cin>> av_copies;
}
show()
{
cout<<"Book title:" << booksTitle <<endl;
cout<<"Authors name:" << author <<endl;
cout<<"Available copies:" << av_copies <<endl;
}
};
int main()
{
card a;
a.set_a();
a.show();
}
output:

Average marks of a student.
#include<iostream>
using namespace std;
class student
{
private :
int x,y,z,avg;
public:
void get_avg_marks(int x,int y,int z)
{
avg=(x+y+z)/3;
cout<<avg;
}
};
int main()
{
student ab;
int x,y,z;
cin>>x>>y>>z;
ab.get_avg_marks(x,y,z);
return 0;
}
output:
A simple class of draw line.
#include<iostream>
using namespace std;
class line
{
private :
int len;
public:
void set_len()
{
cin>>len;
}
void drawLine()
{
cout<<"*";
}
int get_len()
{
return(len);
}
};
int main()
{
line ab;
int n,i;
ab.set_len();
n=ab.get_len();
for(i=0;i<n;i++)
{
ab.drawLine();
}
}
output:
Rectangle class which has two private data:width and length,
#include<iostream>
using namespace std;
class rectangle
{
private :int width,length;
public:
void get_width()
{
cout<<"input width:";
cin>>width;
}
void get_length()
{
cout<<"input length:";
cin>>length;
}
void area()
{
cout<<"Area is="<<(width*length)<<endl;
}
};
int main()
{ rectangle rectangle1,rectangle2;
rectangle1.get_width();
rectangle1.get_length();
rectangle1.area();
rectangle2.get_width();
rectangle2.get_length();
rectangle2.area();
return 0;
}
output:
Comments
Post a Comment