IDE : Qt Creator [get it from here]
first we need to create a proect. inside project directory we will create a class. for creating a new proect, follow the below navigation:
File->new file or proect->Applications->Qt Console Application
then click on 'choose' and give a name to proect ,press next, select a kit (?default will be there), then click next and then finish. a proect will be created. Inside source folder you can see a main.cpp.
Now we can create our class. here i'm going to create a Student class with name,age and rollNo as member variable and member fuctions such as getters,setters,constructor and display.
Navigation for creating a class :
File->new file or project->(under files and classes) C++ ->C++ Class
click on choose, give a name to your class (here 'Student') click next and then finish. It will create 2 files:-
Student.h :-this is a header file. It contain the definition of class file. that is, variable declaration and member function declaration. it will not contain implementaion of these functions. in short, the header file specify the structure of class. Student.h is shown below
class definition will be inside #ifndef STUDENT_H and #endif // STUDENT_H. this is to prvent insertion header file more than once in any othe source file (by mistake insertion) . we use #include to insert a header file in a source file.
Here we specify public/private/protected members of the class.
Student.cpp : it contain implementaion of function header specified in our header file. Student.cpp is shown below
Here we implemented all functions and constructors.
To define any class member outside the class definition requires a scope resolution operator as shown below
ClassName::function_name
Now we can use our class in any of other source file. In below given file, we create two Instance of our student class. we use constructor with parameter to create st , instance of Student. For st2 we use member fuction, setters ,to set value of variables. and finally we use st.show() and to show contents of object st. for st2, we use getters to get value of variable and standard IO to print details.
Main.cpp is shown below
Output is shown below.
first we need to create a proect. inside project directory we will create a class. for creating a new proect, follow the below navigation:
File->new file or proect->Applications->Qt Console Application
then click on 'choose' and give a name to proect ,press next, select a kit (?default will be there), then click next and then finish. a proect will be created. Inside source folder you can see a main.cpp.
Now we can create our class. here i'm going to create a Student class with name,age and rollNo as member variable and member fuctions such as getters,setters,constructor and display.
Navigation for creating a class :
File->new file or project->(under files and classes) C++ ->C++ Class
click on choose, give a name to your class (here 'Student') click next and then finish. It will create 2 files:-
Student.h :-this is a header file. It contain the definition of class file. that is, variable declaration and member function declaration. it will not contain implementaion of these functions. in short, the header file specify the structure of class. Student.h is shown below
class definition will be inside #ifndef STUDENT_H and #endif // STUDENT_H. this is to prvent insertion header file more than once in any othe source file (by mistake insertion) . we use #include to insert a header file in a source file.
Here we specify public/private/protected members of the class.
Student.cpp : it contain implementaion of function header specified in our header file. Student.cpp is shown below
Here we implemented all functions and constructors.
To define any class member outside the class definition requires a scope resolution operator as shown below
ClassName::function_name
Now we can use our class in any of other source file. In below given file, we create two Instance of our student class. we use constructor with parameter to create st , instance of Student. For st2 we use member fuction, setters ,to set value of variables. and finally we use st.show() and to show contents of object st. for st2, we use getters to get value of variable and standard IO to print details.
Main.cpp is shown below
Output is shown below.
No comments:
Post a Comment