Skip to content

Instantly share code, notes, and snippets.

@winhtut
Last active December 29, 2019 13:36
Show Gist options
  • Save winhtut/22346456c4685d710beccdc8c3972710 to your computer and use it in GitHub Desktop.
Save winhtut/22346456c4685d710beccdc8c3972710 to your computer and use it in GitHub Desktop.
Class and Object DeepDive C++
#include<iostream>
using namespace std;
class Dog{
public :
string name;
int age;
float weight;
};
int main(){
Dog bobo;
Dog puppy;
bobo.age=5;
bobo.name="bobo";
puppy.age=3;
bobo.weight=5.5;
puppy.weight=3.2;
cout<<"My Dog name is "<<bobo.name<<endl;
cout<<"Age of bobo"<<bobo.age<<endl;
cout<<"Age of puppy"<<puppy.age<<endl;
return 0;
}
#include <iostream>
using namespace std;
class Book {
private:
int year;
double price;
public:
string title;
void printBook();
void setPrice(double p);
void setYear(int y);
} book;
void Book::setPrice(double p) {
price = p;
}
void Book::setYear(int y) {
year = y;
}
void Book::printBook() {
cout << "Title: " << title << "\n";
cout << "Price: " << price < "\n";
cout << "Year: " << year << "\n";
}
int main() {
book.title = "hooked";
book.setYear(2013);
book.printBook();
return 0;
}
#include <iostream>
using namespace std;
class Book { //declaration a class
private:
int year;
double price;
public:
string title;
void printBook();
void setPrice(double p);
void setYear(int y);
};
void Book::setPrice(double p) {
price = p;
}
void Book::setYear(int y) {
year = y;
}
void Book::printBook() {
cout << "Title of the book : " << title <<endl;
cout << "Price of the book : " << price <<endl;
cout << "Year : " << year << endl;
}
int main() {
Book book;
int bPrice=0;
cout<<"enter title of the book:";
cin >> book.title;
cout<<"enter price of the book:";
cin>>bPrice;
book.setPrice(bPrice);
book.setYear(2013);
book.printBook();
return 0;
}
#include <iostream>
using namespace std;
class Parent {
protected:
int data;
};
class Child : public Parent {
public:
void setData(int id)
{
data = id;
}
void print()
{
cout << "Protected data is: "
<< data << endl;
}
};
int main()
{
Child child;
child.setData(1001);
child.print();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment