Simple program of c++
#include<iostream.h>
class student
{
private:
int roll;
char sem;
public:
void getdata()
{
cout<<"enter the roll number"<<endl;
cin>>roll;
cout<<"enter semester"<<endl;
cin>>sem;
}
void display()
{
cout<<"roll="<<roll<<endl;
cout<<"semester="<<sem<<endl;
}
};
void main()
{
student s;
s.getdata();
s.display();
}
Class:
Way of defining class:
a) Defining function within the class:
class class_name
{
private:
data_type variable1
2
|
|
n
public:
return_type methods()
{
}
|
|
|
};
Example:
#include<iostream.h>
class student
{
private:
int roll;
char name[20];
public:
void getdata()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the roll";
cin>>roll;
}
void display()
{
cout<<"name"<<name<<endl;
cout<<"roll="<<roll<<endl;
}
};
void main()
{
student s;
s.getdata();
s.display();
}
b) Defining function outside the class
class class_name
{
data_type variable 1;
public:
return_type method1();
return_type method2();
n;
};
Example:
#include<iostream.h>
class student
{
int roll;
char name[20];
public:
void getdata();
void display();
};
void student::getdata() (:: is scope resolution operator)
{
cout<<"enter the name";
cin>>name;
cout>>"enter the roll";
cin>>roll;
}
void student::display()
{
cout<<"name"<<name<<endl;
cout<<"roll"<<roll<<endl;
}
void main()
{
student s;
s.getdata();
s.display();
}
#include<iostream.h>
class student
{
private:
int roll;
char sem;
public:
void getdata()
{
cout<<"enter the roll number"<<endl;
cin>>roll;
cout<<"enter semester"<<endl;
cin>>sem;
}
void display()
{
cout<<"roll="<<roll<<endl;
cout<<"semester="<<sem<<endl;
}
};
void main()
{
student s;
s.getdata();
s.display();
}
Class:
Way of defining class:
a) Defining function within the class:
class class_name
{
private:
data_type variable1
2
|
|
n
public:
return_type methods()
{
}
|
|
|
};
Example:
#include<iostream.h>
class student
{
private:
int roll;
char name[20];
public:
void getdata()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the roll";
cin>>roll;
}
void display()
{
cout<<"name"<<name<<endl;
cout<<"roll="<<roll<<endl;
}
};
void main()
{
student s;
s.getdata();
s.display();
}
b) Defining function outside the class
class class_name
{
data_type variable 1;
public:
return_type method1();
return_type method2();
n;
};
Example:
#include<iostream.h>
class student
{
int roll;
char name[20];
public:
void getdata();
void display();
};
void student::getdata() (:: is scope resolution operator)
{
cout<<"enter the name";
cin>>name;
cout>>"enter the roll";
cin>>roll;
}
void student::display()
{
cout<<"name"<<name<<endl;
cout<<"roll"<<roll<<endl;
}
void main()
{
student s;
s.getdata();
s.display();
}
No comments:
Post a Comment